github.com/iotexproject/iotex-core@v1.14.1-rc1/blockchain/genesis/genesis_test.go (about) 1 // Copyright (c) 2019 IoTeX Foundation 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package genesis 7 8 import ( 9 "encoding/hex" 10 "testing" 11 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 15 "github.com/iotexproject/iotex-address/address" 16 ) 17 18 func TestDefaultConfig(t *testing.T) { 19 // construct a config without overriding 20 cfg, err := New("") 21 require.NoError(t, err) 22 // Validate blockchain 23 assert.Equal(t, Default.BlockGasLimit, cfg.BlockGasLimit) 24 assert.Equal(t, Default.ActionGasLimit, cfg.ActionGasLimit) 25 assert.Equal(t, Default.NumSubEpochs, cfg.NumSubEpochs) 26 assert.Equal(t, Default.NumDelegates, cfg.NumDelegates) 27 // Validate rewarding protocol) 28 assert.Equal(t, Default.BlockReward(), cfg.BlockReward()) 29 assert.Equal(t, Default.EpochReward(), cfg.EpochReward()) 30 assert.Equal(t, Default.FoundationBonus(), cfg.FoundationBonus()) 31 } 32 func TestHash(t *testing.T) { 33 require := require.New(t) 34 cfg, err := New("") 35 require.NoError(err) 36 hash := cfg.Hash() 37 require.Equal("3dfcdee76186b59a9f9abd0ded8e6c093c35bddea23834044550fb68626adb62", hex.EncodeToString(hash[:])) 38 } 39 func TestAccount_InitBalances(t *testing.T) { 40 require := require.New(t) 41 InitBalanceMap := make(map[string]string, 0) 42 InitBalanceMap["io1emxf8zzqckhgjde6dqd97ts0y3q496gm3fdrl6"] = "1" 43 InitBalanceMap["io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms"] = "2" 44 acc := Account{InitBalanceMap: InitBalanceMap} 45 adds, balances := acc.InitBalances() 46 require.Equal("io1emxf8zzqckhgjde6dqd97ts0y3q496gm3fdrl6", adds[0].String()) 47 require.Equal("io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms", adds[1].String()) 48 require.Equal(InitBalanceMap["io1emxf8zzqckhgjde6dqd97ts0y3q496gm3fdrl6"], balances[0].Text(10)) 49 require.Equal(InitBalanceMap["io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms"], balances[1].Text(10)) 50 } 51 52 func TestTsunamiBlockGasLimit(t *testing.T) { 53 r := require.New(t) 54 55 cfg := Default 56 for _, v := range []struct { 57 height, gasLimit uint64 58 }{ 59 {1, 20000000}, 60 {cfg.TsunamiBlockHeight - 1, 20000000}, 61 {cfg.TsunamiBlockHeight, 50000000}, 62 {cfg.ToBeEnabledBlockHeight, 50000000}, 63 } { 64 r.Equal(v.gasLimit, cfg.BlockGasLimitByHeight(v.height)) 65 } 66 } 67 68 func TestDeployerWhitelist(t *testing.T) { 69 r := require.New(t) 70 71 cases := []struct { 72 addr string 73 expect bool 74 }{ 75 {"0x3fab184622dc19b6109349b94811493bf2a45362", true}, 76 {"io18743s33zmsvmvyynfxu5sy2f80e2g5mzk3y5ue", true}, 77 {"0x3fab184622dc19b6109349b94811493bf2a45361", false}, 78 {"io18743s33zmsvmvyynfxu5sy2f80e2g5mpcz3zjx", false}, 79 } 80 runTest := func(g *Genesis) { 81 var ( 82 addr address.Address 83 err error 84 ) 85 for _, c := range cases { 86 if c.addr[:2] == "0x" { 87 addr, err = address.FromHex(c.addr) 88 r.NoError(err) 89 } else { 90 addr, err = address.FromString(c.addr) 91 r.NoError(err) 92 } 93 r.Equal(c.expect, g.IsDeployerWhitelisted(addr)) 94 } 95 } 96 t.Run("0x address", func(t *testing.T) { 97 runTest(&Default) 98 }) 99 t.Run("io address", func(t *testing.T) { 100 g := Default 101 g.ReplayDeployerWhitelist = []string{"io18743s33zmsvmvyynfxu5sy2f80e2g5mzk3y5ue"} 102 runTest(&g) 103 }) 104 }