From d392f4e59abd73e7fa72305d2b37c6e98a0294b7 Mon Sep 17 00:00:00 2001 From: Anshal Shukla Date: Tue, 7 Nov 2023 00:49:38 +0530 Subject: [PATCH 1/4] fix: set balance in blockalloc --- consensus/bor/bor.go | 1 + 1 file changed, 1 insertion(+) diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index e4880f7914..7f0e0a2cf2 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -882,6 +882,7 @@ func (c *Bor) changeContractCodeIfNeeded(headerNumber uint64, state *state.State for addr, account := range allocs { log.Info("change contract code", "address", addr) state.SetCode(addr, account.Code) + state.SetBalance(addr, account.Balance) } } } From b78260f3c022062b3fcf4a31d21d7b269b19ab41 Mon Sep 17 00:00:00 2001 From: Anshal Shukla Date: Tue, 7 Nov 2023 11:38:30 +0530 Subject: [PATCH 2/4] chg: don't update balance if not zero --- consensus/bor/bor.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index 7f0e0a2cf2..d6d00211cf 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -882,7 +882,9 @@ func (c *Bor) changeContractCodeIfNeeded(headerNumber uint64, state *state.State for addr, account := range allocs { log.Info("change contract code", "address", addr) state.SetCode(addr, account.Code) - state.SetBalance(addr, account.Balance) + if account.Balance == big.NewInt(0) { + state.SetBalance(addr, account.Balance) + } } } } From 999152a0745b0667051ec65a3ff548965c589ffd Mon Sep 17 00:00:00 2001 From: Anshal Shukla Date: Tue, 7 Nov 2023 12:09:38 +0530 Subject: [PATCH 3/4] fix: lint --- consensus/bor/bor.go | 1 + 1 file changed, 1 insertion(+) diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index d6d00211cf..c3e80f58e9 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -882,6 +882,7 @@ func (c *Bor) changeContractCodeIfNeeded(headerNumber uint64, state *state.State for addr, account := range allocs { log.Info("change contract code", "address", addr) state.SetCode(addr, account.Code) + if account.Balance == big.NewInt(0) { state.SetBalance(addr, account.Balance) } From 74d42cbf58cf6824e5ad6dbf261a313a1636f63f Mon Sep 17 00:00:00 2001 From: Anshal Shukla Date: Thu, 30 Nov 2023 16:28:15 +0530 Subject: [PATCH 4/4] fix logic, add test cases --- consensus/bor/bor.go | 2 +- consensus/bor/bor_test.go | 31 ++++++++++++++++++++++++------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index c3e80f58e9..6a546c637d 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -883,7 +883,7 @@ func (c *Bor) changeContractCodeIfNeeded(headerNumber uint64, state *state.State log.Info("change contract code", "address", addr) state.SetCode(addr, account.Code) - if account.Balance == big.NewInt(0) { + if state.GetBalance(addr).Cmp(big.NewInt(0)) == 0 { state.SetBalance(addr, account.Balance) } } diff --git a/consensus/bor/bor_test.go b/consensus/bor/bor_test.go index 590ddbc3a7..30fa49c1c9 100644 --- a/consensus/bor/bor_test.go +++ b/consensus/bor/bor_test.go @@ -40,6 +40,12 @@ func TestGenesisContractChange(t *testing.T) { "balance": "0x1000", }, }, + "6": map[string]interface{}{ + addr0.Hex(): map[string]interface{}{ + "code": hexutil.Bytes{0x1, 0x4}, + "balance": "0x2000", + }, + }, }, }, } @@ -85,24 +91,35 @@ func TestGenesisContractChange(t *testing.T) { root := genesis.Root() - // code does not change + // code does not change, balance remains 0 root, statedb = addBlock(root, 1) require.Equal(t, statedb.GetCode(addr0), []byte{0x1, 0x1}) + require.Equal(t, statedb.GetBalance(addr0), big.NewInt(0)) - // code changes 1st time + // code changes 1st time, balance remains 0 root, statedb = addBlock(root, 2) require.Equal(t, statedb.GetCode(addr0), []byte{0x1, 0x2}) + require.Equal(t, statedb.GetBalance(addr0), big.NewInt(0)) - // code same as 1st change + // code same as 1st change, balance remains 0 root, statedb = addBlock(root, 3) require.Equal(t, statedb.GetCode(addr0), []byte{0x1, 0x2}) + require.Equal(t, statedb.GetBalance(addr0), big.NewInt(0)) - // code changes 2nd time - _, statedb = addBlock(root, 4) + // code changes 2nd time, balance updates to 4096 + root, statedb = addBlock(root, 4) require.Equal(t, statedb.GetCode(addr0), []byte{0x1, 0x3}) + require.Equal(t, statedb.GetBalance(addr0), big.NewInt(4096)) - // make sure balance change DOES NOT take effect - require.Equal(t, statedb.GetBalance(addr0), big.NewInt(0)) + // code same as 2nd change, balance remains 4096 + root, statedb = addBlock(root, 5) + require.Equal(t, statedb.GetCode(addr0), []byte{0x1, 0x3}) + require.Equal(t, statedb.GetBalance(addr0), big.NewInt(4096)) + + // code changes 3rd time, balance remains 4096 + _, statedb = addBlock(root, 6) + require.Equal(t, statedb.GetCode(addr0), []byte{0x1, 0x4}) + require.Equal(t, statedb.GetBalance(addr0), big.NewInt(4096)) } func TestEncodeSigHeaderJaipur(t *testing.T) {