2023-04-20 10:28:54 +00:00
|
|
|
package state_test
|
2023-03-05 07:33:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
2024-05-29 10:00:12 +00:00
|
|
|
"github.com/holiman/uint256"
|
2023-03-13 19:06:45 +00:00
|
|
|
"github.com/lib/pq"
|
2023-03-05 07:33:51 +00:00
|
|
|
"github.com/multiformats/go-multihash"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2023-09-20 06:16:44 +00:00
|
|
|
"github.com/cerc-io/plugeth-statediff/indexer/database/sql/postgres"
|
|
|
|
"github.com/cerc-io/plugeth-statediff/indexer/ipld"
|
2023-03-05 07:33:51 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
|
2023-04-20 10:28:54 +00:00
|
|
|
state "github.com/cerc-io/ipld-eth-statedb/direct_by_leaf"
|
2023-03-05 07:33:51 +00:00
|
|
|
util "github.com/cerc-io/ipld-eth-statedb/internal"
|
2023-04-20 10:28:54 +00:00
|
|
|
"github.com/cerc-io/ipld-eth-statedb/sql"
|
2023-03-05 07:33:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-04-29 08:41:28 +00:00
|
|
|
testCtx = context.Background()
|
|
|
|
teardownStatements = []string{
|
|
|
|
`TRUNCATE eth.header_cids`,
|
|
|
|
`TRUNCATE eth.state_cids`,
|
|
|
|
`TRUNCATE eth.storage_cids`,
|
|
|
|
`TRUNCATE ipld.blocks`,
|
|
|
|
}
|
2023-03-05 07:33:51 +00:00
|
|
|
|
|
|
|
// Fixture data
|
2023-03-08 18:06:39 +00:00
|
|
|
// block one: contract account and slot are created
|
|
|
|
// block two: slot is emptied
|
|
|
|
// block three: slot has a new value added
|
|
|
|
// block four: non-canonical block with non-canonical slot value is added to the database
|
|
|
|
// block five: entire contract is destructed; another non-canonical block is created but it doesn't include an update for our slot
|
|
|
|
// but it links back to the other non-canonical header (this is to test the ability to resolve canonicity by comparing how many
|
2023-03-08 18:44:49 +00:00
|
|
|
// children reference back to a header)
|
|
|
|
// block six: canonical block only, no relevant state changes (check that we still return emptied result at heights where it wasn't emptied)
|
2023-03-05 07:33:51 +00:00
|
|
|
BlockNumber = big.NewInt(1337)
|
|
|
|
Header = types.Header{Number: BlockNumber}
|
|
|
|
BlockHash = Header.Hash()
|
2023-03-08 17:20:16 +00:00
|
|
|
BlockHash2 = crypto.Keccak256Hash([]byte("I am a random hash"))
|
|
|
|
BlockNumber2 = BlockNumber.Uint64() + 1
|
|
|
|
BlockHash3 = crypto.Keccak256Hash([]byte("I am another random hash"))
|
|
|
|
BlockNumber3 = BlockNumber.Uint64() + 2
|
|
|
|
BlockHash4 = crypto.Keccak256Hash([]byte("I am"))
|
|
|
|
BlockNumber4 = BlockNumber.Uint64() + 3
|
|
|
|
BlockHash5 = crypto.Keccak256Hash([]byte("I"))
|
|
|
|
BlockNumber5 = BlockNumber.Uint64() + 4
|
2023-03-08 18:06:39 +00:00
|
|
|
BlockHash6 = crypto.Keccak256Hash([]byte("am"))
|
|
|
|
BlockNumber6 = BlockNumber.Uint64() + 5
|
2023-03-05 07:33:51 +00:00
|
|
|
BlockParentHash = common.HexToHash("0123456701234567012345670123456701234567012345670123456701234567")
|
|
|
|
|
2023-03-09 15:03:47 +00:00
|
|
|
NonCanonicalHash4 = crypto.Keccak256Hash([]byte("I am a random non canonical hash"))
|
|
|
|
NonCanonicalHash5 = crypto.Keccak256Hash([]byte("I am also a random non canonical hash"))
|
2023-03-08 18:06:39 +00:00
|
|
|
|
2023-03-05 07:33:51 +00:00
|
|
|
AccountPK, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
|
|
|
|
AccountAddress = crypto.PubkeyToAddress(AccountPK.PublicKey) //0x703c4b2bD70c169f5717101CaeE543299Fc946C7
|
2023-04-20 10:28:54 +00:00
|
|
|
AccountLeafKey = crypto.Keccak256Hash(AccountAddress[:])
|
2023-03-05 07:33:51 +00:00
|
|
|
|
|
|
|
AccountCode = []byte{0, 1, 2, 3, 4, 5, 6, 7}
|
|
|
|
AccountCodeHash = crypto.Keccak256Hash(AccountCode)
|
|
|
|
|
|
|
|
Account = types.StateAccount{
|
|
|
|
Nonce: uint64(0),
|
2024-05-29 10:00:12 +00:00
|
|
|
Balance: uint256.NewInt(1000),
|
2023-03-05 07:33:51 +00:00
|
|
|
CodeHash: AccountCodeHash.Bytes(),
|
|
|
|
Root: common.Hash{},
|
|
|
|
}
|
|
|
|
|
2023-04-20 09:29:53 +00:00
|
|
|
StorageSlot = common.HexToHash("0")
|
|
|
|
StorageLeafKey = crypto.Keccak256Hash(StorageSlot[:])
|
2023-03-05 07:33:51 +00:00
|
|
|
StoredValue = crypto.Keccak256Hash([]byte{1, 2, 3, 4, 5})
|
|
|
|
StoragePartialPath = []byte{0, 1, 0, 2, 0, 4}
|
|
|
|
|
|
|
|
// Encoded data
|
|
|
|
accountRLP, _ = rlp.EncodeToBytes(&Account)
|
|
|
|
accountAndLeafRLP, _ = rlp.EncodeToBytes(&[]interface{}{AccountLeafKey, accountRLP})
|
|
|
|
AccountCID, _ = ipld.RawdataToCid(ipld.MEthStateTrie, accountAndLeafRLP, multihash.KECCAK_256)
|
2023-04-20 10:28:54 +00:00
|
|
|
AccountCodeCID, _ = util.Keccak256ToCid(ipld.RawBinary, AccountCodeHash[:])
|
2023-03-05 07:33:51 +00:00
|
|
|
|
2023-03-08 18:44:49 +00:00
|
|
|
StoredValueRLP, _ = rlp.EncodeToBytes(StoredValue)
|
|
|
|
StoredValueRLP2, _ = rlp.EncodeToBytes("something")
|
2023-03-08 18:06:39 +00:00
|
|
|
NonCanonStoredValueRLP, _ = rlp.EncodeToBytes("something else")
|
2023-03-08 18:44:49 +00:00
|
|
|
StorageRLP, _ = rlp.EncodeToBytes(&[]interface{}{StoragePartialPath, StoredValueRLP})
|
|
|
|
StorageCID, _ = ipld.RawdataToCid(ipld.MEthStorageTrie, StorageRLP, multihash.KECCAK_256)
|
2023-03-08 18:06:39 +00:00
|
|
|
|
2023-03-08 02:41:19 +00:00
|
|
|
RemovedNodeStateCID = "baglacgzayxjemamg64rtzet6pwznzrydydsqbnstzkbcoo337lmaixmfurya"
|
|
|
|
RemovedNodeStorageCID = "bagmacgzayxjemamg64rtzet6pwznzrydydsqbnstzkbcoo337lmaixmfurya"
|
2023-03-05 07:33:51 +00:00
|
|
|
)
|
|
|
|
|
2023-03-13 19:06:45 +00:00
|
|
|
func TestPGXSuite(t *testing.T) {
|
2023-09-20 06:16:44 +00:00
|
|
|
testConfig, err := postgres.TestConfig.WithEnv()
|
2023-03-05 07:33:51 +00:00
|
|
|
require.NoError(t, err)
|
2023-04-11 09:42:11 +00:00
|
|
|
pool, err := postgres.ConnectPGX(testCtx, testConfig)
|
2023-03-05 07:33:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Cleanup(func() {
|
|
|
|
tx, err := pool.Begin(testCtx)
|
|
|
|
require.NoError(t, err)
|
2023-04-29 08:41:28 +00:00
|
|
|
for _, stm := range teardownStatements {
|
2023-03-05 07:33:51 +00:00
|
|
|
_, err = tx.Exec(testCtx, stm)
|
|
|
|
require.NoErrorf(t, err, "Exec(`%s`)", stm)
|
|
|
|
}
|
|
|
|
require.NoError(t, tx.Commit(testCtx))
|
|
|
|
})
|
2023-03-13 19:06:45 +00:00
|
|
|
|
2023-04-20 10:28:54 +00:00
|
|
|
database := sql.NewPGXDriverFromPool(context.Background(), pool)
|
2023-04-11 09:42:11 +00:00
|
|
|
insertSuiteData(t, database)
|
2023-03-13 19:06:45 +00:00
|
|
|
|
2024-05-29 10:00:12 +00:00
|
|
|
db := state.NewDatabase(database)
|
2023-04-11 09:42:11 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
testSuite(t, db)
|
2023-03-13 19:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSQLXSuite(t *testing.T) {
|
2023-09-20 06:16:44 +00:00
|
|
|
testConfig, err := postgres.TestConfig.WithEnv()
|
2023-03-13 19:06:45 +00:00
|
|
|
require.NoError(t, err)
|
2023-04-11 09:42:11 +00:00
|
|
|
pool, err := postgres.ConnectSQLX(testCtx, testConfig)
|
2023-03-13 19:06:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Cleanup(func() {
|
|
|
|
tx, err := pool.Begin()
|
|
|
|
require.NoError(t, err)
|
2023-04-29 08:41:28 +00:00
|
|
|
for _, stm := range teardownStatements {
|
2023-03-13 19:06:45 +00:00
|
|
|
_, err = tx.Exec(stm)
|
|
|
|
require.NoErrorf(t, err, "Exec(`%s`)", stm)
|
|
|
|
}
|
|
|
|
require.NoError(t, tx.Commit())
|
|
|
|
})
|
2023-04-11 09:42:11 +00:00
|
|
|
|
2023-04-20 10:28:54 +00:00
|
|
|
database := sql.NewSQLXDriverFromPool(context.Background(), pool)
|
2023-04-11 09:42:11 +00:00
|
|
|
insertSuiteData(t, database)
|
|
|
|
|
2024-05-29 10:00:12 +00:00
|
|
|
db := state.NewDatabase(database)
|
2023-04-11 09:42:11 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
testSuite(t, db)
|
|
|
|
}
|
|
|
|
|
2023-04-20 10:28:54 +00:00
|
|
|
func insertSuiteData(t *testing.T, database sql.Database) {
|
2023-09-20 06:16:44 +00:00
|
|
|
require.NoError(t, insertHeaderCID(database, BlockHash.String(), BlockParentHash.String(), BlockNumber.Uint64(), true))
|
|
|
|
require.NoError(t, insertHeaderCID(database, BlockHash2.String(), BlockHash.String(), BlockNumber2, true))
|
|
|
|
require.NoError(t, insertHeaderCID(database, BlockHash3.String(), BlockHash2.String(), BlockNumber3, true))
|
|
|
|
require.NoError(t, insertHeaderCID(database, BlockHash4.String(), BlockHash3.String(), BlockNumber4, true))
|
|
|
|
require.NoError(t, insertHeaderCID(database, NonCanonicalHash4.String(), BlockHash3.String(), BlockNumber4, false))
|
|
|
|
require.NoError(t, insertHeaderCID(database, BlockHash5.String(), BlockHash4.String(), BlockNumber5, true))
|
|
|
|
require.NoError(t, insertHeaderCID(database, NonCanonicalHash5.String(), NonCanonicalHash4.String(), BlockNumber5, false))
|
|
|
|
require.NoError(t, insertHeaderCID(database, BlockHash6.String(), BlockHash5.String(), BlockNumber6, true))
|
2023-03-13 19:06:45 +00:00
|
|
|
require.NoError(t, insertStateCID(database, stateModel{
|
2023-03-08 02:41:19 +00:00
|
|
|
BlockNumber: BlockNumber.Uint64(),
|
|
|
|
BlockHash: BlockHash.String(),
|
|
|
|
LeafKey: AccountLeafKey.String(),
|
|
|
|
CID: AccountCID.String(),
|
|
|
|
Diff: true,
|
|
|
|
Balance: Account.Balance.Uint64(),
|
|
|
|
Nonce: Account.Nonce,
|
|
|
|
CodeHash: AccountCodeHash.String(),
|
|
|
|
StorageRoot: Account.Root.String(),
|
|
|
|
Removed: false,
|
|
|
|
}))
|
2023-03-13 19:06:45 +00:00
|
|
|
require.NoError(t, insertStateCID(database, stateModel{
|
2023-03-09 15:03:47 +00:00
|
|
|
BlockNumber: BlockNumber4,
|
|
|
|
BlockHash: NonCanonicalHash4.String(),
|
2023-03-08 18:44:49 +00:00
|
|
|
LeafKey: AccountLeafKey.String(),
|
|
|
|
CID: AccountCID.String(),
|
|
|
|
Diff: true,
|
2023-03-09 15:03:47 +00:00
|
|
|
Balance: big.NewInt(123).Uint64(),
|
2023-03-08 18:44:49 +00:00
|
|
|
Nonce: Account.Nonce,
|
|
|
|
CodeHash: AccountCodeHash.String(),
|
|
|
|
StorageRoot: Account.Root.String(),
|
|
|
|
Removed: false,
|
|
|
|
}))
|
2023-03-13 19:06:45 +00:00
|
|
|
require.NoError(t, insertStateCID(database, stateModel{
|
2023-03-08 17:20:16 +00:00
|
|
|
BlockNumber: BlockNumber5,
|
|
|
|
BlockHash: BlockHash5.String(),
|
2023-03-08 02:41:19 +00:00
|
|
|
LeafKey: AccountLeafKey.String(),
|
2023-03-08 17:20:16 +00:00
|
|
|
CID: RemovedNodeStateCID,
|
2023-03-08 02:41:19 +00:00
|
|
|
Diff: true,
|
|
|
|
Removed: true,
|
|
|
|
}))
|
2023-03-13 19:06:45 +00:00
|
|
|
require.NoError(t, insertStorageCID(database, storageModel{
|
2023-03-08 02:41:19 +00:00
|
|
|
BlockNumber: BlockNumber.Uint64(),
|
|
|
|
BlockHash: BlockHash.String(),
|
|
|
|
LeafKey: AccountLeafKey.String(),
|
|
|
|
StorageLeafKey: StorageLeafKey.String(),
|
|
|
|
StorageCID: StorageCID.String(),
|
|
|
|
Diff: true,
|
|
|
|
Value: StoredValueRLP,
|
|
|
|
Removed: false,
|
|
|
|
}))
|
2023-03-13 19:06:45 +00:00
|
|
|
require.NoError(t, insertStorageCID(database, storageModel{
|
2023-03-08 17:20:16 +00:00
|
|
|
BlockNumber: BlockNumber2,
|
|
|
|
BlockHash: BlockHash2.String(),
|
2023-03-08 02:41:19 +00:00
|
|
|
LeafKey: AccountLeafKey.String(),
|
|
|
|
StorageLeafKey: StorageLeafKey.String(),
|
2023-03-08 17:20:16 +00:00
|
|
|
StorageCID: RemovedNodeStorageCID,
|
2023-03-08 02:41:19 +00:00
|
|
|
Diff: true,
|
|
|
|
Value: []byte{},
|
|
|
|
Removed: true,
|
|
|
|
}))
|
2023-03-13 19:06:45 +00:00
|
|
|
require.NoError(t, insertStorageCID(database, storageModel{
|
2023-03-08 17:20:16 +00:00
|
|
|
BlockNumber: BlockNumber3,
|
|
|
|
BlockHash: BlockHash3.String(),
|
2023-03-08 02:41:19 +00:00
|
|
|
LeafKey: AccountLeafKey.String(),
|
|
|
|
StorageLeafKey: StorageLeafKey.String(),
|
|
|
|
StorageCID: StorageCID.String(),
|
|
|
|
Diff: true,
|
|
|
|
Value: StoredValueRLP2,
|
|
|
|
Removed: false,
|
|
|
|
}))
|
2023-03-13 19:06:45 +00:00
|
|
|
require.NoError(t, insertStorageCID(database, storageModel{
|
2023-03-08 18:06:39 +00:00
|
|
|
BlockNumber: BlockNumber4,
|
|
|
|
BlockHash: NonCanonicalHash4.String(),
|
|
|
|
LeafKey: AccountLeafKey.String(),
|
|
|
|
StorageLeafKey: StorageLeafKey.String(),
|
|
|
|
StorageCID: StorageCID.String(),
|
|
|
|
Diff: true,
|
|
|
|
Value: NonCanonStoredValueRLP,
|
|
|
|
Removed: false,
|
|
|
|
}))
|
2023-03-13 19:06:45 +00:00
|
|
|
require.NoError(t, insertContractCode(database))
|
2023-04-11 09:42:11 +00:00
|
|
|
}
|
2023-03-05 07:33:51 +00:00
|
|
|
|
2024-05-29 10:00:12 +00:00
|
|
|
func testSuite(t *testing.T, db state.Database) {
|
2023-03-05 07:33:51 +00:00
|
|
|
t.Run("Database", func(t *testing.T) {
|
2023-03-06 17:46:07 +00:00
|
|
|
size, err := db.ContractCodeSize(AccountCodeHash)
|
2023-03-05 07:33:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(AccountCode), size)
|
|
|
|
|
2023-03-06 17:46:07 +00:00
|
|
|
code, err := db.ContractCode(AccountCodeHash)
|
2023-03-05 07:33:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, AccountCode, code)
|
|
|
|
|
|
|
|
acct, err := db.StateAccount(AccountLeafKey, BlockHash)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, &Account, acct)
|
|
|
|
|
2023-03-08 17:20:16 +00:00
|
|
|
acct2, err := db.StateAccount(AccountLeafKey, BlockHash2)
|
2023-03-08 02:41:19 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, &Account, acct2)
|
|
|
|
|
2023-03-08 17:20:16 +00:00
|
|
|
acct3, err := db.StateAccount(AccountLeafKey, BlockHash3)
|
2023-03-08 02:41:19 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, &Account, acct3)
|
|
|
|
|
2023-03-08 18:44:49 +00:00
|
|
|
// check that we don't get the non-canonical account
|
2023-03-08 17:20:16 +00:00
|
|
|
acct4, err := db.StateAccount(AccountLeafKey, BlockHash4)
|
2023-03-08 02:41:19 +00:00
|
|
|
require.NoError(t, err)
|
2023-03-08 16:03:48 +00:00
|
|
|
require.Equal(t, &Account, acct4)
|
|
|
|
|
2023-03-08 17:20:16 +00:00
|
|
|
acct5, err := db.StateAccount(AccountLeafKey, BlockHash5)
|
2023-03-08 16:03:48 +00:00
|
|
|
require.NoError(t, err)
|
2023-03-08 17:20:16 +00:00
|
|
|
require.Nil(t, acct5)
|
2023-03-08 02:41:19 +00:00
|
|
|
|
2023-03-08 18:06:39 +00:00
|
|
|
acct6, err := db.StateAccount(AccountLeafKey, BlockHash6)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, acct6)
|
|
|
|
|
2023-03-05 07:33:51 +00:00
|
|
|
val, err := db.StorageValue(AccountLeafKey, StorageLeafKey, BlockHash)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, StoredValueRLP, val)
|
2023-03-08 02:41:19 +00:00
|
|
|
|
2023-03-08 17:20:16 +00:00
|
|
|
val2, err := db.StorageValue(AccountLeafKey, StorageLeafKey, BlockHash2)
|
2023-03-08 02:41:19 +00:00
|
|
|
require.NoError(t, err)
|
2023-03-08 17:20:16 +00:00
|
|
|
require.Nil(t, val2)
|
2023-03-08 02:41:19 +00:00
|
|
|
|
2023-03-08 17:20:16 +00:00
|
|
|
val3, err := db.StorageValue(AccountLeafKey, StorageLeafKey, BlockHash3)
|
2023-03-08 02:41:19 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, StoredValueRLP2, val3)
|
|
|
|
|
2023-03-08 18:06:39 +00:00
|
|
|
// this checks that we don't get the non-canonical result
|
2023-03-08 17:20:16 +00:00
|
|
|
val4, err := db.StorageValue(AccountLeafKey, StorageLeafKey, BlockHash4)
|
2023-03-08 02:41:19 +00:00
|
|
|
require.NoError(t, err)
|
2023-03-08 16:03:48 +00:00
|
|
|
require.Equal(t, StoredValueRLP2, val4)
|
|
|
|
|
2023-03-08 18:44:49 +00:00
|
|
|
// this checks that when the entire account was deleted, we return nil result for storage slot
|
2023-03-08 17:20:16 +00:00
|
|
|
val5, err := db.StorageValue(AccountLeafKey, StorageLeafKey, BlockHash5)
|
2023-03-08 16:03:48 +00:00
|
|
|
require.NoError(t, err)
|
2023-03-08 17:20:16 +00:00
|
|
|
require.Nil(t, val5)
|
2023-03-08 18:06:39 +00:00
|
|
|
|
|
|
|
val6, err := db.StorageValue(AccountLeafKey, StorageLeafKey, BlockHash6)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, val6)
|
2023-03-05 07:33:51 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("StateDB", func(t *testing.T) {
|
2023-04-20 10:28:54 +00:00
|
|
|
sdb, err := state.New(BlockHash, db)
|
2023-03-05 07:33:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
checkAccountUnchanged := func() {
|
|
|
|
require.Equal(t, Account.Balance, sdb.GetBalance(AccountAddress))
|
|
|
|
require.Equal(t, Account.Nonce, sdb.GetNonce(AccountAddress))
|
2023-04-20 09:29:53 +00:00
|
|
|
require.Equal(t, StoredValue, sdb.GetState(AccountAddress, StorageSlot))
|
2023-03-05 07:33:51 +00:00
|
|
|
require.Equal(t, AccountCodeHash, sdb.GetCodeHash(AccountAddress))
|
|
|
|
require.Equal(t, AccountCode, sdb.GetCode(AccountAddress))
|
|
|
|
require.Equal(t, len(AccountCode), sdb.GetCodeSize(AccountAddress))
|
|
|
|
}
|
|
|
|
|
|
|
|
require.True(t, sdb.Exist(AccountAddress))
|
|
|
|
checkAccountUnchanged()
|
|
|
|
|
|
|
|
id := sdb.Snapshot()
|
|
|
|
|
|
|
|
newStorage := crypto.Keccak256Hash([]byte{5, 4, 3, 2, 1})
|
|
|
|
newCode := []byte{1, 3, 3, 7}
|
|
|
|
|
2024-05-29 10:00:12 +00:00
|
|
|
sdb.SetBalance(AccountAddress, uint256.NewInt(300))
|
|
|
|
sdb.AddBalance(AccountAddress, uint256.NewInt(200))
|
|
|
|
sdb.SubBalance(AccountAddress, uint256.NewInt(100))
|
2023-03-05 07:33:51 +00:00
|
|
|
sdb.SetNonce(AccountAddress, 42)
|
2023-04-20 09:29:53 +00:00
|
|
|
sdb.SetState(AccountAddress, StorageSlot, newStorage)
|
2023-03-05 07:33:51 +00:00
|
|
|
sdb.SetCode(AccountAddress, newCode)
|
|
|
|
|
2024-05-29 10:00:12 +00:00
|
|
|
require.Equal(t, uint256.NewInt(400), sdb.GetBalance(AccountAddress))
|
2023-03-05 07:33:51 +00:00
|
|
|
require.Equal(t, uint64(42), sdb.GetNonce(AccountAddress))
|
2023-04-20 09:29:53 +00:00
|
|
|
require.Equal(t, newStorage, sdb.GetState(AccountAddress, StorageSlot))
|
2023-03-05 07:33:51 +00:00
|
|
|
require.Equal(t, newCode, sdb.GetCode(AccountAddress))
|
|
|
|
|
2023-04-20 09:29:53 +00:00
|
|
|
sdb.AddSlotToAccessList(AccountAddress, StorageSlot)
|
2023-03-05 07:33:51 +00:00
|
|
|
require.True(t, sdb.AddressInAccessList(AccountAddress))
|
2023-04-20 09:29:53 +00:00
|
|
|
hasAddr, hasSlot := sdb.SlotInAccessList(AccountAddress, StorageSlot)
|
2023-03-05 07:33:51 +00:00
|
|
|
require.True(t, hasAddr)
|
|
|
|
require.True(t, hasSlot)
|
|
|
|
|
|
|
|
sdb.RevertToSnapshot(id)
|
|
|
|
|
|
|
|
checkAccountUnchanged()
|
|
|
|
require.False(t, sdb.AddressInAccessList(AccountAddress))
|
2023-04-20 09:29:53 +00:00
|
|
|
hasAddr, hasSlot = sdb.SlotInAccessList(AccountAddress, StorageSlot)
|
2023-03-05 07:33:51 +00:00
|
|
|
require.False(t, hasAddr)
|
|
|
|
require.False(t, hasSlot)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-09-20 06:16:44 +00:00
|
|
|
func insertHeaderCID(db sql.Database, blockHash, parentHash string, blockNumber uint64, canon bool) error {
|
2023-03-08 18:06:39 +00:00
|
|
|
cid, err := util.Keccak256ToCid(ipld.MEthHeader, common.HexToHash(blockHash).Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-05 07:33:51 +00:00
|
|
|
sql := `INSERT INTO eth.header_cids (
|
|
|
|
block_number,
|
|
|
|
block_hash,
|
|
|
|
parent_hash,
|
|
|
|
cid,
|
|
|
|
td,
|
|
|
|
node_ids,
|
|
|
|
reward,
|
|
|
|
state_root,
|
|
|
|
tx_root,
|
|
|
|
receipt_root,
|
|
|
|
uncles_hash,
|
|
|
|
bloom,
|
|
|
|
timestamp,
|
2023-09-20 06:16:44 +00:00
|
|
|
coinbase,
|
|
|
|
canonical
|
|
|
|
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15)`
|
2023-03-08 18:06:39 +00:00
|
|
|
_, err = db.Exec(testCtx, sql,
|
2023-03-08 02:41:19 +00:00
|
|
|
blockNumber,
|
|
|
|
blockHash,
|
2023-03-08 18:06:39 +00:00
|
|
|
parentHash,
|
|
|
|
cid.String(),
|
2023-03-13 19:06:45 +00:00
|
|
|
0, pq.StringArray([]string{}), 0,
|
2023-03-05 07:33:51 +00:00
|
|
|
Header.Root.String(),
|
|
|
|
Header.TxHash.String(),
|
|
|
|
Header.ReceiptHash.String(),
|
2023-03-08 17:20:16 +00:00
|
|
|
Header.UncleHash.String(),
|
2023-03-05 07:33:51 +00:00
|
|
|
[]byte{},
|
|
|
|
Header.Time,
|
|
|
|
Header.Coinbase.String(),
|
2023-09-20 06:16:44 +00:00
|
|
|
canon,
|
2023-03-05 07:33:51 +00:00
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-08 02:41:19 +00:00
|
|
|
type stateModel struct {
|
|
|
|
BlockNumber uint64
|
|
|
|
BlockHash string
|
|
|
|
LeafKey string
|
|
|
|
CID string
|
|
|
|
Diff bool
|
|
|
|
Balance uint64
|
|
|
|
Nonce uint64
|
|
|
|
CodeHash string
|
|
|
|
StorageRoot string
|
|
|
|
Removed bool
|
|
|
|
}
|
|
|
|
|
2023-04-20 10:28:54 +00:00
|
|
|
func insertStateCID(db sql.Database, cidModel stateModel) error {
|
2023-03-05 07:33:51 +00:00
|
|
|
sql := `INSERT INTO eth.state_cids (
|
|
|
|
block_number,
|
|
|
|
header_id,
|
|
|
|
state_leaf_key,
|
|
|
|
cid,
|
|
|
|
diff,
|
|
|
|
balance,
|
|
|
|
nonce,
|
|
|
|
code_hash,
|
|
|
|
storage_root,
|
|
|
|
removed
|
|
|
|
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)`
|
|
|
|
_, err := db.Exec(testCtx, sql,
|
2023-03-08 02:41:19 +00:00
|
|
|
cidModel.BlockNumber,
|
|
|
|
cidModel.BlockHash,
|
|
|
|
cidModel.LeafKey,
|
|
|
|
cidModel.CID,
|
|
|
|
cidModel.Diff,
|
|
|
|
cidModel.Balance,
|
|
|
|
cidModel.Nonce,
|
|
|
|
cidModel.CodeHash,
|
|
|
|
cidModel.StorageRoot,
|
|
|
|
cidModel.Removed,
|
2023-03-05 07:33:51 +00:00
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-08 02:41:19 +00:00
|
|
|
type storageModel struct {
|
|
|
|
BlockNumber uint64
|
|
|
|
BlockHash string
|
|
|
|
LeafKey string
|
|
|
|
StorageLeafKey string
|
|
|
|
StorageCID string
|
|
|
|
Diff bool
|
|
|
|
Value []byte
|
|
|
|
Removed bool
|
|
|
|
}
|
|
|
|
|
2023-04-20 10:28:54 +00:00
|
|
|
func insertStorageCID(db sql.Database, cidModel storageModel) error {
|
2023-03-05 07:33:51 +00:00
|
|
|
sql := `INSERT INTO eth.storage_cids (
|
|
|
|
block_number,
|
|
|
|
header_id,
|
|
|
|
state_leaf_key,
|
|
|
|
storage_leaf_key,
|
|
|
|
cid,
|
|
|
|
diff,
|
|
|
|
val,
|
|
|
|
removed
|
|
|
|
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`
|
|
|
|
_, err := db.Exec(testCtx, sql,
|
2023-03-08 02:41:19 +00:00
|
|
|
cidModel.BlockNumber,
|
|
|
|
cidModel.BlockHash,
|
|
|
|
cidModel.LeafKey,
|
|
|
|
cidModel.StorageLeafKey,
|
|
|
|
cidModel.StorageCID,
|
|
|
|
cidModel.Diff,
|
|
|
|
cidModel.Value,
|
|
|
|
cidModel.Removed,
|
2023-03-05 07:33:51 +00:00
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-20 10:28:54 +00:00
|
|
|
func insertContractCode(db sql.Database) error {
|
2023-03-05 07:33:51 +00:00
|
|
|
sql := `INSERT INTO ipld.blocks (block_number, key, data) VALUES ($1, $2, $3)`
|
2023-03-13 19:06:45 +00:00
|
|
|
_, err := db.Exec(testCtx, sql, BlockNumber.Uint64(), AccountCodeCID.String(), AccountCode)
|
2023-03-05 07:33:51 +00:00
|
|
|
return err
|
|
|
|
}
|