Skip to content

Commit

Permalink
feat(state): enable Ed25519 for the Testnet (#1497)
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f authored Sep 4, 2024
1 parent 1dc4092 commit 1c6ddfe
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
4 changes: 4 additions & 0 deletions genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func (n ChainType) IsMainnet() bool {
return n == Mainnet
}

func (n ChainType) IsTestnet() bool {
return n == Testnet
}

func (n ChainType) String() string {
switch n {
case Mainnet:
Expand Down
40 changes: 21 additions & 19 deletions genesis/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,6 @@ func TestMarshaling(t *testing.T) {
assert.Error(t, err, "file not found")
}

func TestGenesisTestnet(t *testing.T) {
crypto.AddressHRP = "tpc"

gen := genesis.TestnetGenesis()
assert.Equal(t, 4, len(gen.Validators()))
assert.Equal(t, 5, len(gen.Accounts()))

genTime, _ := time.Parse("2006-01-02", "2024-03-16")
expected, _ := hash.FromString("13f96e6fbc9e0de0d53537ac5e894fc8e66be1600436db2df1511dc30696e822")
assert.Equal(t, expected, gen.Hash())
assert.Equal(t, genTime, gen.GenesisTime())
assert.Equal(t, uint32(360), gen.Params().BondInterval)
assert.Equal(t, genesis.Testnet, gen.ChainType())
assert.Equal(t, amount.Amount(42e15), gen.TotalSupply())

// reset address HRP global variable to miannet to prevent next tests failing.
crypto.AddressHRP = "pc"
}

func TestGenesisMainnet(t *testing.T) {
gen := genesis.MainnetGenesis()
assert.Equal(t, len(gen.Validators()), 4)
Expand All @@ -79,6 +60,7 @@ func TestGenesisMainnet(t *testing.T) {
assert.Equal(t, uint32(8640*21), gen.Params().UnbondInterval)
assert.Equal(t, genesis.Mainnet, gen.ChainType())
assert.Equal(t, amount.Amount(42e15), gen.TotalSupply())
assert.True(t, gen.ChainType().IsMainnet())
}

func TestCheckGenesisAccountAndValidator(t *testing.T) {
Expand All @@ -104,3 +86,23 @@ func TestCheckGenesisAccountAndValidator(t *testing.T) {
assert.Equal(t, vals[i].Hash(), val.Hash())
}
}

func TestGenesisTestnet(t *testing.T) {
crypto.AddressHRP = "tpc"

gen := genesis.TestnetGenesis()
assert.Equal(t, 4, len(gen.Validators()))
assert.Equal(t, 5, len(gen.Accounts()))

genTime, _ := time.Parse("2006-01-02", "2024-03-16")
expected, _ := hash.FromString("13f96e6fbc9e0de0d53537ac5e894fc8e66be1600436db2df1511dc30696e822")
assert.Equal(t, expected, gen.Hash())
assert.Equal(t, genTime, gen.GenesisTime())
assert.Equal(t, uint32(360), gen.Params().BondInterval)
assert.Equal(t, genesis.Testnet, gen.ChainType())
assert.Equal(t, amount.Amount(42e15), gen.TotalSupply())
assert.True(t, gen.ChainType().IsTestnet())

// reset address HRP global variable to miannet to prevent next tests failing.
crypto.AddressHRP = "pc"
}
23 changes: 23 additions & 0 deletions state/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func (st *state) executeBlock(b *block.Block, sb sandbox.Sandbox, check bool) er
}

if check {
if err := st.checkEd25519Fork(trx); err != nil {
return err
}

err := execution.CheckAndExecute(trx, sb, true)
if err != nil {
return err
Expand All @@ -52,3 +56,22 @@ func (st *state) executeBlock(b *block.Block, sb sandbox.Sandbox, check bool) er

return nil
}

func (st *state) checkEd25519Fork(trx *tx.Tx) error {
// TODO: remove me after enabling Ed255519
if trx.Payload().Signer().Type() == crypto.AddressTypeEd25519Account {
if st.genDoc.ChainType().IsMainnet() {
return errors.Errorf(errors.ErrInvalidTx,
"ed255519 not supported yet")
}

if st.genDoc.ChainType().IsTestnet() {
if st.lastInfo.BlockHeight() < 1_320_000 {
return errors.Errorf(errors.ErrInvalidTx,
"ed255519 not supported yet")
}
}
}

return nil
}

0 comments on commit 1c6ddfe

Please sign in to comment.