MCPcopy Create free account
hub / github.com/CPChain/chain / TestCopy

Function TestCopy

core/state/statedb_test.go:122–167  ·  view source on GitHub ↗

TestCopy tests that copying a statedb object indeed makes the original and the copy independent of each other. This test is a regression test against https://github.com/ethereum/go-ethereum/pull/15549.

(t *testing.T)

Source from the content-addressed store, hash-verified

120// the copy independent of each other. This test is a regression test against
121// https://github.com/ethereum/go-ethereum/pull/15549.
122func TestCopy(t *testing.T) {
123 // Create a random state test to copy and modify "independently"
124 orig, _ := New(common.Hash{}, NewDatabase(database.NewMemDatabase()))
125
126 for i := byte(0); i < 255; i++ {
127 obj := orig.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
128 obj.AddBalance(big.NewInt(int64(i)))
129 orig.updateStateObject(obj)
130 }
131 orig.Finalise(false)
132
133 // Copy the state, modify both in-memory
134 copy := orig.Copy()
135
136 for i := byte(0); i < 255; i++ {
137 origObj := orig.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
138 copyObj := copy.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
139
140 origObj.AddBalance(big.NewInt(2 * int64(i)))
141 copyObj.AddBalance(big.NewInt(3 * int64(i)))
142
143 orig.updateStateObject(origObj)
144 copy.updateStateObject(copyObj)
145 }
146 // Finalise the changes on both concurrently
147 done := make(chan struct{})
148 go func() {
149 orig.Finalise(true)
150 close(done)
151 }()
152 copy.Finalise(true)
153 <-done
154
155 // Verify that the two states have been updated independently
156 for i := byte(0); i < 255; i++ {
157 origObj := orig.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
158 copyObj := copy.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
159
160 if want := big.NewInt(3 * int64(i)); origObj.Balance().Cmp(want) != 0 {
161 t.Errorf("orig obj %d: balance mismatch: have %v, want %v", i, origObj.Balance(), want)
162 }
163 if want := big.NewInt(4 * int64(i)); copyObj.Balance().Cmp(want) != 0 {
164 t.Errorf("copy obj %d: balance mismatch: have %v, want %v", i, copyObj.Balance(), want)
165 }
166 }
167}
168
169func TestSnapshotRandom(t *testing.T) {
170 config := &quick.Config{MaxCount: 1000}

Callers

nothing calls this directly

Calls 9

NewDatabaseFunction · 0.85
GetOrNewStateObjectMethod · 0.80
updateStateObjectMethod · 0.80
FinaliseMethod · 0.80
CmpMethod · 0.80
NewFunction · 0.70
AddBalanceMethod · 0.65
CopyMethod · 0.45
BalanceMethod · 0.45

Tested by

no test coverage detected