Skip to content

Commit

Permalink
add missing genesis (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 authored Oct 18, 2024
1 parent b8164ad commit 1581f89
Show file tree
Hide file tree
Showing 4 changed files with 893 additions and 97 deletions.
40 changes: 34 additions & 6 deletions proto/minievm/evm/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,36 @@ message GenesisState {
(gogoproto.nullable) = false
];

// erc20 contracts
repeated bytes erc20s = 3 [
(gogoproto.moretags) = "yaml:\"erc20s\"",
(gogoproto.customname) = "ERC20s"
];

// erc20 stores
repeated GenesisERC20Stores erc20_stores = 3 [
repeated GenesisERC20Stores erc20_stores = 4 [
(gogoproto.moretags) = "yaml:\"erc20_stores\"",
(gogoproto.nullable) = false
];

repeated GenesisDenomAddress denom_addresses = 4 [
(gogoproto.moretags) = "yaml:\"denom_addresses\"",
repeated GenesisDenomTrace denom_traces = 5 [
(gogoproto.moretags) = "yaml:\"denom_traces\"",
(gogoproto.nullable) = false
];

repeated GenesisClassTrace class_traces = 6 [
(gogoproto.moretags) = "yaml:\"class_traces\"",
(gogoproto.nullable) = false
];

repeated GenesisEVMBlockHash evm_block_hashes = 7 [
(gogoproto.moretags) = "yaml:\"evm_block_hashes\"",
(gogoproto.customname) = "EVMBlockHashes",
(gogoproto.nullable) = false
];

// erc20 factory contract address
bytes erc20_factory = 5;
bytes erc20_factory = 8;
}

// GenesisKeyValue defines store KV values.
Expand All @@ -44,8 +61,19 @@ message GenesisERC20Stores {
repeated bytes stores = 2;
}

// GenesisDenomAddress defines erc20 contract address of denom.
message GenesisDenomAddress {
// GenesisDenomTrace defines erc20 contract address of denom.
message GenesisDenomTrace {
string denom = 1;
bytes contract_address = 2;
}

message GenesisClassTrace {
string class_id = 1;
bytes contract_address = 2;
string uri = 3;
}

message GenesisEVMBlockHash {
bytes hash = 1;
uint64 height = 2;
}
82 changes: 76 additions & 6 deletions x/evm/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ func (k Keeper) InitGenesis(ctx context.Context, genState types.GenesisState) er
}
}

for _, erc20 := range genState.ERC20s {
if err := k.ERC20s.Set(ctx, erc20); err != nil {
return err
}
}

for _, stores := range genState.Erc20Stores {
for _, store := range stores.Stores {
if err := k.ERC20Stores.Set(ctx, collections.Join(stores.Address, store)); err != nil {
Expand All @@ -81,12 +87,32 @@ func (k Keeper) InitGenesis(ctx context.Context, genState types.GenesisState) er
}
}

for _, denomAddress := range genState.DenomAddresses {
if err := k.ERC20ContractAddrsByDenom.Set(ctx, denomAddress.Denom, denomAddress.ContractAddress); err != nil {
for _, denomTrace := range genState.DenomTraces {
if err := k.ERC20ContractAddrsByDenom.Set(ctx, denomTrace.Denom, denomTrace.ContractAddress); err != nil {
return err
}

if err := k.ERC20DenomsByContractAddr.Set(ctx, denomTrace.ContractAddress, denomTrace.Denom); err != nil {
return err
}
}

for _, classTrace := range genState.ClassTraces {
if err := k.ERC721ContractAddrsByClassId.Set(ctx, classTrace.ClassId, classTrace.ContractAddress); err != nil {
return err
}

if err := k.ERC20DenomsByContractAddr.Set(ctx, denomAddress.ContractAddress, denomAddress.Denom); err != nil {
if err := k.ERC721ClassIdsByContractAddr.Set(ctx, classTrace.ContractAddress, classTrace.ClassId); err != nil {
return err
}

if err := k.ERC721ClassURIs.Set(ctx, classTrace.ContractAddress, classTrace.Uri); err != nil {
return err
}
}

for _, blockHash := range genState.EVMBlockHashes {
if err := k.EVMBlockHashes.Set(ctx, blockHash.Height, blockHash.Hash); err != nil {
return err
}
}
Expand All @@ -109,6 +135,15 @@ func (k Keeper) ExportGenesis(ctx context.Context) *types.GenesisState {
panic(err)
}

erc20s := [][]byte{}
err = k.ERC20s.Walk(ctx, nil, func(erc20 []byte) (stop bool, err error) {
erc20s = append(erc20s, erc20)
return false, nil
})
if err != nil {
panic(err)
}

var stores *types.GenesisERC20Stores
erc20Stores := []types.GenesisERC20Stores{}
err = k.ERC20Stores.Walk(ctx, nil, func(key collections.Pair[[]byte, []byte]) (stop bool, err error) {
Expand All @@ -129,9 +164,9 @@ func (k Keeper) ExportGenesis(ctx context.Context) *types.GenesisState {
panic(err)
}

denomAddresses := []types.GenesisDenomAddress{}
denomTraces := []types.GenesisDenomTrace{}
err = k.ERC20ContractAddrsByDenom.Walk(ctx, nil, func(denom string, contractAddr []byte) (stop bool, err error) {
denomAddresses = append(denomAddresses, types.GenesisDenomAddress{
denomTraces = append(denomTraces, types.GenesisDenomTrace{
Denom: denom,
ContractAddress: contractAddr,
})
Expand All @@ -142,6 +177,38 @@ func (k Keeper) ExportGenesis(ctx context.Context) *types.GenesisState {
panic(err)
}

classTraces := []types.GenesisClassTrace{}
err = k.ERC721ContractAddrsByClassId.Walk(ctx, nil, func(classId string, contractAddr []byte) (stop bool, err error) {
uri, err := k.ERC721ClassURIs.Get(ctx, contractAddr)
if err != nil {
panic(err)
}

classTraces = append(classTraces, types.GenesisClassTrace{
ClassId: classId,
ContractAddress: contractAddr,
Uri: uri,
})

return false, nil
})
if err != nil {
panic(err)
}

evmBlockHashes := []types.GenesisEVMBlockHash{}
err = k.EVMBlockHashes.Walk(ctx, nil, func(height uint64, hash []byte) (stop bool, err error) {
evmBlockHashes = append(evmBlockHashes, types.GenesisEVMBlockHash{
Height: height,
Hash: hash,
})

return false, nil
})
if err != nil {
panic(err)
}

factoryAddr, err := k.ERC20FactoryAddr.Get(ctx)
if err != nil {
panic(err)
Expand All @@ -150,8 +217,11 @@ func (k Keeper) ExportGenesis(ctx context.Context) *types.GenesisState {
return &types.GenesisState{
Params: params,
KeyValues: kvs,
ERC20s: erc20s,
Erc20Stores: erc20Stores,
DenomAddresses: denomAddresses,
DenomTraces: denomTraces,
ClassTraces: classTraces,
Erc20Factory: factoryAddr,
EVMBlockHashes: evmBlockHashes,
}
}
28 changes: 27 additions & 1 deletion x/evm/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ func Test_Genesis(t *testing.T) {

genState := types.DefaultGenesis()
genState.Erc20Factory = []byte{5, 6, 7, 8}
genState.ERC20s = [][]byte{
{1, 2, 3},
{4, 5, 6},
}
genState.Erc20Stores = []types.GenesisERC20Stores{
{
Address: []byte{1, 2, 3},
Expand All @@ -36,7 +40,7 @@ func Test_Genesis(t *testing.T) {
Value: []byte{1, 2, 3, 5, 32, 4},
},
}
genState.DenomAddresses = []types.GenesisDenomAddress{
genState.DenomTraces = []types.GenesisDenomTrace{
{
Denom: "denom1",
ContractAddress: []byte{1, 2, 3, 4, 5, 6, 7},
Expand All @@ -46,6 +50,28 @@ func Test_Genesis(t *testing.T) {
ContractAddress: []byte{8, 9, 1, 2, 3, 4},
},
}
genState.ClassTraces = []types.GenesisClassTrace{
{
ClassId: "class1",
ContractAddress: []byte{1, 2, 3, 4, 5, 6, 7},
Uri: "uri1",
},
{
ClassId: "class2",
ContractAddress: []byte{8, 9, 1, 2, 3, 4},
Uri: "uri2",
},
}
genState.EVMBlockHashes = []types.GenesisEVMBlockHash{
{
Height: 1,
Hash: []byte{1, 2, 3, 4, 5, 6, 7},
},
{
Height: 2,
Hash: []byte{8, 9, 1, 2, 3, 4},
},
}
err := input.EVMKeeper.InitGenesis(ctx, *genState)
require.NoError(t, err)

Expand Down
Loading

0 comments on commit 1581f89

Please sign in to comment.