Skip to content

Commit 74f6b61

Browse files
authored
Merge pull request #628 from guagualvcha/fix_state_inconsistent
[R4R]fix state inconsistent when doing diffsync
2 parents 0b57544 + d7e48bf commit 74f6b61

File tree

5 files changed

+109
-19
lines changed

5 files changed

+109
-19
lines changed

.github/release.env

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
MAINNET_FILE_URL="https://github.com/binance-chain/bsc/releases/download/v1.1.2/mainnet.zip"
2-
TESTNET_FILE_URL="https://github.com/binance-chain/bsc/releases/download/v1.1.2/testnet.zip"
1+
MAINNET_FILE_URL="https://github.com/binance-chain/bsc/releases/download/v1.1.6/mainnet.zip"
2+
TESTNET_FILE_URL="https://github.com/binance-chain/bsc/releases/download/v1.1.6/testnet.zip"

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## v1.1.7
4+
5+
BUGFIX
6+
* [\#628](https://github.com/binance-chain/bsc/pull/628) fix state inconsistent when doing diffsync
7+
38
## v1.1.6
49
BUGFIX
510
* [\#582](https://github.com/binance-chain/bsc/pull/582) the DoS vulnerabilities fixed in go-ethereum v1.10.9

core/blockchain_diff_test.go

+100-15
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
package core
2222

2323
import (
24+
"bytes"
25+
"encoding/hex"
2426
"math/big"
2527
"testing"
2628
"time"
@@ -42,17 +44,51 @@ import (
4244

4345
var (
4446
// testKey is a private key to use for funding a tester account.
45-
testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
47+
testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
48+
contractCode, _ = hex.DecodeString("608060405260016000806101000a81548160ff02191690831515021790555034801561002a57600080fd5b506101688061003a6000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c806389a2d8011461003b578063b0483f4814610059575b600080fd5b610043610075565b60405161005091906100f4565b60405180910390f35b610073600480360381019061006e91906100bc565b61008b565b005b60008060009054906101000a900460ff16905090565b806000806101000a81548160ff02191690831515021790555050565b6000813590506100b68161011b565b92915050565b6000602082840312156100ce57600080fd5b60006100dc848285016100a7565b91505092915050565b6100ee8161010f565b82525050565b600060208201905061010960008301846100e5565b92915050565b60008115159050919050565b6101248161010f565b811461012f57600080fd5b5056fea264697066735822122092f788b569bfc3786e90601b5dbec01cfc3d76094164fd66ca7d599c4239fc5164736f6c63430008000033")
49+
contractAddr = common.HexToAddress("0xe74a3c7427cda785e0000d42a705b1f3fd371e09")
50+
contractSlot = common.HexToHash("0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563")
51+
contractData1, _ = hex.DecodeString("b0483f480000000000000000000000000000000000000000000000000000000000000000")
52+
contractData2, _ = hex.DecodeString("b0483f480000000000000000000000000000000000000000000000000000000000000001")
53+
commonGas = 192138
4654
// testAddr is the Ethereum address of the tester account.
4755
testAddr = crypto.PubkeyToAddress(testKey.PublicKey)
56+
57+
checkBlocks = map[int]checkBlockParam{
58+
12: {
59+
txs: []checkTransactionParam{
60+
{
61+
to: &contractAddr,
62+
slot: contractSlot,
63+
value: []byte{01},
64+
},
65+
}},
66+
67+
13: {
68+
txs: []checkTransactionParam{
69+
{
70+
to: &contractAddr,
71+
slot: contractSlot,
72+
value: []byte{},
73+
},
74+
}},
75+
14: {
76+
txs: []checkTransactionParam{
77+
{
78+
to: &contractAddr,
79+
slot: contractSlot,
80+
value: []byte{01},
81+
},
82+
}},
83+
}
4884
// testBlocks is the test parameters array for specific blocks.
4985
testBlocks = []testBlockParam{
5086
{
5187
// This txs params also used to default block.
5288
blockNr: 11,
5389
txs: []testTransactionParam{
5490
{
55-
to: common.Address{0x01},
91+
to: &common.Address{0x01},
5692
value: big.NewInt(1),
5793
gasPrice: big.NewInt(1),
5894
data: nil,
@@ -63,51 +99,74 @@ var (
6399
blockNr: 12,
64100
txs: []testTransactionParam{
65101
{
66-
to: common.Address{0x01},
102+
to: &common.Address{0x01},
67103
value: big.NewInt(1),
68104
gasPrice: big.NewInt(1),
69105
data: nil,
70106
},
71107
{
72-
to: common.Address{0x02},
108+
to: &common.Address{0x02},
73109
value: big.NewInt(2),
74110
gasPrice: big.NewInt(2),
75111
data: nil,
76112
},
113+
{
114+
to: nil,
115+
value: big.NewInt(0),
116+
gasPrice: big.NewInt(2),
117+
data: contractCode,
118+
},
77119
},
78120
},
79121
{
80122
blockNr: 13,
81123
txs: []testTransactionParam{
82124
{
83-
to: common.Address{0x01},
125+
to: &common.Address{0x01},
84126
value: big.NewInt(1),
85127
gasPrice: big.NewInt(1),
86128
data: nil,
87129
},
88130
{
89-
to: common.Address{0x02},
131+
to: &common.Address{0x02},
90132
value: big.NewInt(2),
91133
gasPrice: big.NewInt(2),
92134
data: nil,
93135
},
94136
{
95-
to: common.Address{0x03},
137+
to: &common.Address{0x03},
96138
value: big.NewInt(3),
97139
gasPrice: big.NewInt(3),
98140
data: nil,
99141
},
142+
{
143+
to: &contractAddr,
144+
value: big.NewInt(0),
145+
gasPrice: big.NewInt(3),
146+
data: contractData1,
147+
},
100148
},
101149
},
102150
{
103151
blockNr: 14,
152+
txs: []testTransactionParam{
153+
{
154+
to: &contractAddr,
155+
value: big.NewInt(0),
156+
gasPrice: big.NewInt(3),
157+
data: contractData2,
158+
},
159+
},
160+
},
161+
{
162+
blockNr: 15,
104163
txs: []testTransactionParam{},
105164
},
106165
}
107166
)
108167

109168
type testTransactionParam struct {
110-
to common.Address
169+
to *common.Address
111170
value *big.Int
112171
gasPrice *big.Int
113172
data []byte
@@ -118,6 +177,16 @@ type testBlockParam struct {
118177
txs []testTransactionParam
119178
}
120179

180+
type checkTransactionParam struct {
181+
to *common.Address
182+
slot common.Hash
183+
value []byte
184+
}
185+
186+
type checkBlockParam struct {
187+
txs []checkTransactionParam
188+
}
189+
121190
// testBackend is a mock implementation of the live Ethereum message handler. Its
122191
// purpose is to allow testing the request/reply workflows and wire serialization
123192
// in the `eth` protocol without actually doing any data processing.
@@ -153,8 +222,15 @@ func newTestBackendWithGenerator(blocks int, lightProcess bool) *testBackend {
153222
// Specific block setting, the index in this generator has 1 diff from specified blockNr.
154223
if i+1 == testBlock.blockNr {
155224
for _, testTransaction := range testBlock.txs {
156-
tx, err := types.SignTx(types.NewTransaction(block.TxNonce(testAddr), testTransaction.to,
157-
testTransaction.value, params.TxGas, testTransaction.gasPrice, testTransaction.data), signer, testKey)
225+
var transaction *types.Transaction
226+
if testTransaction.to == nil {
227+
transaction = types.NewContractCreation(block.TxNonce(testAddr),
228+
testTransaction.value, uint64(commonGas), testTransaction.gasPrice, testTransaction.data)
229+
} else {
230+
transaction = types.NewTransaction(block.TxNonce(testAddr), *testTransaction.to,
231+
testTransaction.value, uint64(commonGas), testTransaction.gasPrice, testTransaction.data)
232+
}
233+
tx, err := types.SignTx(transaction, signer, testKey)
158234
if err != nil {
159235
panic(err)
160236
}
@@ -168,8 +244,8 @@ func newTestBackendWithGenerator(blocks int, lightProcess bool) *testBackend {
168244
// We want to simulate an empty middle block, having the same state as the
169245
// first one. The last is needs a state change again to force a reorg.
170246
for _, testTransaction := range testBlocks[0].txs {
171-
tx, err := types.SignTx(types.NewTransaction(block.TxNonce(testAddr), testTransaction.to,
172-
testTransaction.value, params.TxGas, testTransaction.gasPrice, testTransaction.data), signer, testKey)
247+
tx, err := types.SignTx(types.NewTransaction(block.TxNonce(testAddr), *testTransaction.to,
248+
testTransaction.value, uint64(commonGas), testTransaction.gasPrice, testTransaction.data), signer, testKey)
173249
if err != nil {
174250
panic(err)
175251
}
@@ -241,6 +317,14 @@ func TestProcessDiffLayer(t *testing.T) {
241317
lightBackend.Chain().HandleDiffLayer(diff, "testpid", true)
242318
}
243319
_, err := lightBackend.chain.insertChain([]*types.Block{block}, true)
320+
if checks, exist := checkBlocks[i]; exist {
321+
for _, check := range checks.txs {
322+
s, _ := lightBackend.Chain().Snapshots().Snapshot(block.Root()).Storage(crypto.Keccak256Hash((*check.to)[:]), check.slot)
323+
if !bytes.Equal(s, check.value) {
324+
t.Fatalf("Expected value %x, get %x", check.value, s)
325+
}
326+
}
327+
}
244328
if err != nil {
245329
t.Errorf("failed to insert block %v", err)
246330
}
@@ -385,13 +469,14 @@ func TestGetDiffAccounts(t *testing.T) {
385469
t.Errorf("the diff accounts does't include addr: %v", testAddr)
386470
}
387471
}
388-
389472
for _, transaction := range testBlock.txs {
473+
if transaction.to == nil || len(transaction.data) > 0 {
474+
continue
475+
}
390476
for idx, account := range accounts {
391-
if transaction.to == account {
477+
if *transaction.to == account {
392478
break
393479
}
394-
395480
if idx == len(accounts)-1 {
396481
t.Errorf("the diff accounts does't include addr: %v", transaction.to)
397482
}

core/state_processor.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ func (p *LightStateProcessor) LightProcess(diffLayer *types.DiffLayer, block *ty
253253

254254
//update storage
255255
latestRoot := common.BytesToHash(latestAccount.Root)
256-
if latestRoot != previousAccount.Root && latestRoot != types.EmptyRootHash {
256+
if latestRoot != previousAccount.Root {
257257
accountTrie, err := statedb.Database().OpenStorageTrie(addrHash, previousAccount.Root)
258258
if err != nil {
259259
errChan <- err

params/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
const (
2424
VersionMajor = 1 // Major version component of the current release
2525
VersionMinor = 1 // Minor version component of the current release
26-
VersionPatch = 6 // Patch version component of the current release
26+
VersionPatch = 7 // Patch version component of the current release
2727
VersionMeta = "" // Version metadata to append to the version string
2828
)
2929

0 commit comments

Comments
 (0)