|
| 1 | +package execution |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "math/big" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/hyperledger/burrow/acm" |
| 10 | + "github.com/hyperledger/burrow/acm/acmstate" |
| 11 | + "github.com/hyperledger/burrow/bcm" |
| 12 | + "github.com/hyperledger/burrow/crypto" |
| 13 | + "github.com/hyperledger/burrow/execution/engine" |
| 14 | + "github.com/hyperledger/burrow/execution/evm" |
| 15 | + "github.com/hyperledger/burrow/execution/evm/abi" |
| 16 | + "github.com/hyperledger/burrow/execution/exec" |
| 17 | + "github.com/hyperledger/burrow/execution/solidity" |
| 18 | + "github.com/hyperledger/burrow/execution/state" |
| 19 | + "github.com/hyperledger/burrow/genesis" |
| 20 | + "github.com/hyperledger/burrow/permission" |
| 21 | + "github.com/stretchr/testify/require" |
| 22 | + dbm "github.com/tendermint/tm-db" |
| 23 | + "golang.org/x/sync/errgroup" |
| 24 | +) |
| 25 | + |
| 26 | +var genesisDoc, _, _ = genesis.NewDeterministicGenesis(100).GenesisDoc(1, 1) |
| 27 | + |
| 28 | +// This test looks at caching problems that arise when doing concurrent reads via CallSim. It requires a cold cache |
| 29 | +// for bug to be exhibited. |
| 30 | +// The root cause of the original bug was a race condition that could lead to reading a uninitialised tree from the |
| 31 | +// MutableForest tree cache. |
| 32 | +func TestCallSimDelegate(t *testing.T) { |
| 33 | + // Roll up our sleeves and swear fealty to the witch-king |
| 34 | + ctx, cancel := context.WithCancel(context.Background()) |
| 35 | + defer cancel() |
| 36 | + g, ctx := errgroup.WithContext(ctx) |
| 37 | + |
| 38 | + db := dbm.NewMemDB() |
| 39 | + st, err := state.MakeGenesisState(db, genesisDoc) |
| 40 | + require.NoError(t, err) |
| 41 | + |
| 42 | + from := crypto.PrivateKeyFromSecret("raaah", crypto.CurveTypeEd25519) |
| 43 | + contractAddress := crypto.Address{1, 2, 3, 4, 5} |
| 44 | + blockchain := &bcm.Blockchain{} |
| 45 | + sink := exec.NewNoopEventSink() |
| 46 | + |
| 47 | + // Function to set storage value for later |
| 48 | + setDelegate := func(up state.Updatable, value crypto.Address) error { |
| 49 | + call, _, err := abi.EncodeFunctionCall(string(solidity.Abi_DelegateProxy), "setDelegate", logger, value) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + cache := acmstate.NewCache(st) |
| 55 | + _, err = evm.Default().Execute(cache, blockchain, sink, |
| 56 | + engine.CallParams{ |
| 57 | + CallType: exec.CallTypeCall, |
| 58 | + Origin: from.GetAddress(), |
| 59 | + Caller: from.GetAddress(), |
| 60 | + Callee: contractAddress, |
| 61 | + Input: call, |
| 62 | + Gas: big.NewInt(9999999), |
| 63 | + }, solidity.DeployedBytecode_DelegateProxy) |
| 64 | + |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + return cache.Sync(up) |
| 69 | + } |
| 70 | + |
| 71 | + // Initialise sender smart contract state |
| 72 | + _, _, err = st.Update(func(up state.Updatable) error { |
| 73 | + err = up.UpdateAccount(&acm.Account{ |
| 74 | + Address: from.GetAddress(), |
| 75 | + PublicKey: from.GetPublicKey(), |
| 76 | + Balance: 9999999, |
| 77 | + Permissions: permission.DefaultAccountPermissions, |
| 78 | + }) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + return up.UpdateAccount(&acm.Account{ |
| 83 | + Address: contractAddress, |
| 84 | + EVMCode: solidity.DeployedBytecode_DelegateProxy, |
| 85 | + }) |
| 86 | + }) |
| 87 | + require.NoError(t, err) |
| 88 | + |
| 89 | + // Set a series of values of storage slot so we get a deep version tree (which we need to trigger the bug) |
| 90 | + delegate := crypto.Address{0xBE, 0xEF, 0, 0xFA, 0xCE, 0, 0xBA, 0} |
| 91 | + for i := 0; i < 0xBF; i++ { |
| 92 | + delegate[7] = byte(i) |
| 93 | + _, _, err = st.Update(func(up state.Updatable) error { |
| 94 | + return setDelegate(up, delegate) |
| 95 | + }) |
| 96 | + require.NoError(t, err) |
| 97 | + } |
| 98 | + |
| 99 | + // This is important in order to illicit the former bug - we need a cold LRU tree cache in MutableForest |
| 100 | + st, err = state.LoadState(db, st.Version()) |
| 101 | + require.NoError(t, err) |
| 102 | + |
| 103 | + getIntCall, _, err := abi.EncodeFunctionCall(string(solidity.Abi_DelegateProxy), "getDelegate", logger) |
| 104 | + require.NoError(t, err) |
| 105 | + n := 1000 |
| 106 | + |
| 107 | + for i := 0; i < n; i++ { |
| 108 | + g.Go(func() error { |
| 109 | + txe, err := CallSim(st, blockchain, from.GetAddress(), contractAddress, getIntCall, logger) |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + err = txe.GetException().AsError() |
| 114 | + if err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + address, err := crypto.AddressFromBytes(txe.GetResult().Return[12:]) |
| 118 | + if err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + if address != delegate { |
| 122 | + // The bug for which this test was written will return the zero address here since it is accessing |
| 123 | + // an uninitialised tree |
| 124 | + return fmt.Errorf("getDelegate returned %v but expected %v", address, delegate) |
| 125 | + } |
| 126 | + return nil |
| 127 | + }) |
| 128 | + } |
| 129 | + |
| 130 | + require.NoError(t, g.Wait()) |
| 131 | +} |
0 commit comments