github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/gossip/c_block_callbacks_test.go (about) 1 package gossip 2 3 import ( 4 "fmt" 5 "math/big" 6 "testing" 7 8 "github.com/stretchr/testify/require" 9 "github.com/unicornultrafoundation/go-helios/native/idx" 10 "github.com/unicornultrafoundation/go-u2u/core/types" 11 12 "github.com/unicornultrafoundation/go-u2u/logger" 13 "github.com/unicornultrafoundation/go-u2u/utils" 14 ) 15 16 func TestConsensusCallback(t *testing.T) { 17 logger.SetTestMode(t) 18 require := require.New(t) 19 20 const rounds = 30 21 22 const validatorsNum = 3 23 24 env := newTestEnv(2, validatorsNum) 25 defer env.Close() 26 27 // save start balances 28 balances := make([]*big.Int, validatorsNum) 29 for i := range balances { 30 balances[i] = env.State().GetBalance(env.Address(idx.ValidatorID(i + 1))) 31 } 32 33 for n := uint64(0); n < rounds; n++ { 34 // transfers 35 txs := make([]*types.Transaction, validatorsNum) 36 for i := idx.Validator(0); i < validatorsNum; i++ { 37 from := i % validatorsNum 38 to := 0 39 txs[i] = env.Transfer(idx.ValidatorID(from+1), idx.ValidatorID(to+1), utils.ToU2U(100)) 40 } 41 tm := sameEpoch 42 if n%10 == 0 { 43 tm = nextEpoch 44 } 45 rr, err := env.ApplyTxs(tm, txs...) 46 require.NoError(err) 47 // subtract fees 48 for i, r := range rr { 49 fee := big.NewInt(0).Mul(new(big.Int).SetUint64(r.GasUsed), txs[i].GasPrice()) 50 balances[i] = big.NewInt(0).Sub(balances[i], fee) 51 } 52 // balance movements 53 balances[0].Add(balances[0], utils.ToU2U(200)) 54 balances[1].Sub(balances[1], utils.ToU2U(100)) 55 balances[2].Sub(balances[2], utils.ToU2U(100)) 56 } 57 58 // check balances 59 for i := range balances { 60 require.Equal( 61 balances[i], 62 env.State().GetBalance(env.Address(idx.ValidatorID(i+1))), 63 fmt.Sprintf("account%d", i), 64 ) 65 } 66 67 }