-
Notifications
You must be signed in to change notification settings - Fork 1
/
keeper.go
156 lines (128 loc) · 4.15 KB
/
keeper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package keeper
import (
"bytes"
"fmt"
"strings"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core"
ethtypes "github.com/ethereum/go-ethereum/core/types"
evmtypes "github.com/rarimo/rarimo-core/x/evm/types"
"github.com/rarimo/rarimo-core/x/identity/pkg/state"
"github.com/rarimo/rarimo-core/x/identity/types"
"github.com/tendermint/tendermint/libs/log"
)
type (
Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
rarimo types.RarimocoreKeeper
}
)
func NewKeeper(
cdc codec.BinaryCodec,
storeKey,
memKey storetypes.StoreKey,
rarimo types.RarimocoreKeeper,
) *Keeper {
return &Keeper{
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
rarimo: rarimo,
}
}
var _ evmtypes.EvmHooks = Keeper{}
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
func (k Keeper) UpdateIdentity(ctx sdk.Context, gist string, id string, hash string) {
treap := Treap{k}
lcg := LCG{k}
// Getting the state by index
state, ok := k.GetStateInfo(ctx, id)
if ok {
currentKey := hexutil.Encode(state.CalculateHash())
treap.Remove(ctx, currentKey)
}
state = types.StateInfo{
Index: id,
Hash: hash,
CreatedAtTimestamp: uint64(ctx.BlockTime().UTC().Unix()),
CreatedAtBlock: uint64(ctx.BlockHeight()),
}
// Saving updated state, updating GIST and changed states list.
k.SetStateInfo(ctx, state)
k.SetGIST(ctx, gist)
k.AddToWaitingList(ctx, id)
// Inserting into Merkle tree. Insert method already manages how to act if state already exists.
key := hexutil.Encode(state.CalculateHash())
priority := lcg.Next(ctx)
treap.Insert(ctx, key, priority)
return
}
// Path returns the Merkle path for the requested state hash by state index.
func (k Keeper) Path(ctx sdk.Context, id string) []string {
treap := Treap{k}
state, ok := k.GetStateInfo(ctx, id)
if !ok {
return nil
}
return treap.MerklePath(ctx, hexutil.Encode(state.CalculateHash()))
}
// PostTxProcessing is used to listen EVM smart contract events,
// filter and process `StateTransited` events emitted by configured in module params contract address.
// Will be called by EVM module as hook.
func (k Keeper) PostTxProcessing(ctx sdk.Context, msg core.Message, receipt *ethtypes.Receipt) error {
params := k.GetParams(ctx)
const eventName = "StateTransited"
stateV2, err := abi.JSON(strings.NewReader(state.StateABI))
if err != nil {
return err
}
// Validating message receiver address (should be our state smart contract)
if msg.To() == nil || bytes.Compare(msg.To().Bytes(), hexutil.MustDecode(params.IdentityContractAddress)) != 0 {
return nil
}
// https://docs.evmos.org/protocol/modules/evm#posttxprocessing
for _, log := range receipt.Logs {
eventId := log.Topics[0]
event, err := stateV2.EventByID(eventId)
if err != nil {
continue
}
if event.Name != eventName {
continue
}
eventBody := state.StateStateTransited{}
if err := unpackLog(stateV2, &eventBody, event.Name, log); err != nil {
return err
}
k.Logger(ctx).Info(fmt.Sprintf("Received PostTxProcessing event in %s module: %v", types.ModuleName, eventBody))
// Updating identity Merkle tree
k.UpdateIdentity(ctx, hexutil.Encode(eventBody.GistRoot.Bytes()), hexutil.Encode(eventBody.Id.Bytes()), hexutil.Encode(eventBody.State.Bytes()))
}
return nil
}
// unpackLog copy-pasted from logic in generated s-c bindings.
func unpackLog(contractAbi abi.ABI, out interface{}, event string, log *ethtypes.Log) error {
if log.Topics[0] != contractAbi.Events[event].ID {
return fmt.Errorf("event signature mismatch")
}
if len(log.Data) > 0 {
if err := contractAbi.UnpackIntoInterface(out, event, log.Data); err != nil {
return err
}
}
var indexed abi.Arguments
for _, arg := range contractAbi.Events[event].Inputs {
if arg.Indexed {
indexed = append(indexed, arg)
}
}
return abi.ParseTopics(out, indexed, log.Topics[1:])
}