add simple unit test cases to test behavior when slots or contracts are destructed
This commit is contained in:
parent
bd7a73afe8
commit
affaeeb481
169
statedb_test.go
169
statedb_test.go
@ -28,12 +28,14 @@ var (
|
|||||||
BlockNumber = big.NewInt(1337)
|
BlockNumber = big.NewInt(1337)
|
||||||
Header = types.Header{Number: BlockNumber}
|
Header = types.Header{Number: BlockNumber}
|
||||||
BlockHash = Header.Hash()
|
BlockHash = Header.Hash()
|
||||||
|
RandomHash = crypto.Keccak256Hash([]byte("I am a random hash"))
|
||||||
|
RandomHash2 = crypto.Keccak256Hash([]byte("I am another random hash"))
|
||||||
|
RandomHash3 = crypto.Keccak256Hash([]byte("I"))
|
||||||
BlockParentHash = common.HexToHash("0123456701234567012345670123456701234567012345670123456701234567")
|
BlockParentHash = common.HexToHash("0123456701234567012345670123456701234567012345670123456701234567")
|
||||||
|
|
||||||
AccountPK, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
|
AccountPK, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
|
||||||
AccountAddress = crypto.PubkeyToAddress(AccountPK.PublicKey) //0x703c4b2bD70c169f5717101CaeE543299Fc946C7
|
AccountAddress = crypto.PubkeyToAddress(AccountPK.PublicKey) //0x703c4b2bD70c169f5717101CaeE543299Fc946C7
|
||||||
AccountLeafKey = crypto.Keccak256Hash(AccountAddress.Bytes())
|
AccountLeafKey = crypto.Keccak256Hash(AccountAddress.Bytes())
|
||||||
AccountPath = []byte{AccountLeafKey[0] & 0xf0} // first nibble of path
|
|
||||||
|
|
||||||
AccountCode = []byte{0, 1, 2, 3, 4, 5, 6, 7}
|
AccountCode = []byte{0, 1, 2, 3, 4, 5, 6, 7}
|
||||||
AccountCodeHash = crypto.Keccak256Hash(AccountCode)
|
AccountCodeHash = crypto.Keccak256Hash(AccountCode)
|
||||||
@ -59,8 +61,12 @@ var (
|
|||||||
AccountCodeCID, _ = util.Keccak256ToCid(ipld.RawBinary, AccountCodeHash.Bytes())
|
AccountCodeCID, _ = util.Keccak256ToCid(ipld.RawBinary, AccountCodeHash.Bytes())
|
||||||
|
|
||||||
StoredValueRLP, _ = rlp.EncodeToBytes(StoredValue)
|
StoredValueRLP, _ = rlp.EncodeToBytes(StoredValue)
|
||||||
|
StoredValueRLP2, _ = rlp.EncodeToBytes("something")
|
||||||
StorageRLP, _ = rlp.EncodeToBytes(&[]interface{}{StoragePartialPath, StoredValueRLP})
|
StorageRLP, _ = rlp.EncodeToBytes(&[]interface{}{StoragePartialPath, StoredValueRLP})
|
||||||
StorageCID, _ = ipld.RawdataToCid(ipld.MEthStorageTrie, StorageRLP, multihash.KECCAK_256)
|
StorageCID, _ = ipld.RawdataToCid(ipld.MEthStorageTrie, StorageRLP, multihash.KECCAK_256)
|
||||||
|
|
||||||
|
RemovedNodeStateCID = "baglacgzayxjemamg64rtzet6pwznzrydydsqbnstzkbcoo337lmaixmfurya"
|
||||||
|
RemovedNodeStorageCID = "bagmacgzayxjemamg64rtzet6pwznzrydydsqbnstzkbcoo337lmaixmfurya"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSuite(t *testing.T) {
|
func TestSuite(t *testing.T) {
|
||||||
@ -87,9 +93,70 @@ func TestSuite(t *testing.T) {
|
|||||||
require.NoError(t, tx.Commit(testCtx))
|
require.NoError(t, tx.Commit(testCtx))
|
||||||
})
|
})
|
||||||
|
|
||||||
require.NoError(t, insertHeaderCID(pool))
|
require.NoError(t, insertHeaderCID(pool, BlockHash.String(), BlockNumber.Uint64()))
|
||||||
require.NoError(t, insertStateCID(pool))
|
require.NoError(t, insertHeaderCID(pool, RandomHash.String(), BlockNumber.Uint64()+1))
|
||||||
require.NoError(t, insertStorageCID(pool))
|
require.NoError(t, insertHeaderCID(pool, RandomHash2.String(), BlockNumber.Uint64()+2))
|
||||||
|
require.NoError(t, insertHeaderCID(pool, RandomHash3.String(), BlockNumber.Uint64()+3))
|
||||||
|
require.NoError(t, insertStateCID(pool, stateModel{
|
||||||
|
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,
|
||||||
|
}))
|
||||||
|
require.NoError(t, insertStateCID(pool, stateModel{
|
||||||
|
BlockNumber: BlockNumber.Uint64() + 3,
|
||||||
|
BlockHash: RandomHash3.String(),
|
||||||
|
LeafKey: AccountLeafKey.String(),
|
||||||
|
CID: RemovedNodeStorageCID,
|
||||||
|
Diff: true,
|
||||||
|
Removed: true,
|
||||||
|
}))
|
||||||
|
require.NoError(t, insertStorageCID(pool, storageModel{
|
||||||
|
BlockNumber: BlockNumber.Uint64(),
|
||||||
|
BlockHash: BlockHash.String(),
|
||||||
|
LeafKey: AccountLeafKey.String(),
|
||||||
|
StorageLeafKey: StorageLeafKey.String(),
|
||||||
|
StorageCID: StorageCID.String(),
|
||||||
|
Diff: true,
|
||||||
|
Value: StoredValueRLP,
|
||||||
|
Removed: false,
|
||||||
|
}))
|
||||||
|
require.NoError(t, insertStorageCID(pool, storageModel{
|
||||||
|
BlockNumber: BlockNumber.Uint64() + 1,
|
||||||
|
BlockHash: RandomHash.String(),
|
||||||
|
LeafKey: AccountLeafKey.String(),
|
||||||
|
StorageLeafKey: StorageLeafKey.String(),
|
||||||
|
StorageCID: RemovedNodeStateCID,
|
||||||
|
Diff: true,
|
||||||
|
Value: []byte{},
|
||||||
|
Removed: true,
|
||||||
|
}))
|
||||||
|
require.NoError(t, insertStorageCID(pool, storageModel{
|
||||||
|
BlockNumber: BlockNumber.Uint64() + 2,
|
||||||
|
BlockHash: RandomHash2.String(),
|
||||||
|
LeafKey: AccountLeafKey.String(),
|
||||||
|
StorageLeafKey: StorageLeafKey.String(),
|
||||||
|
StorageCID: StorageCID.String(),
|
||||||
|
Diff: true,
|
||||||
|
Value: StoredValueRLP2,
|
||||||
|
Removed: false,
|
||||||
|
}))
|
||||||
|
require.NoError(t, insertStorageCID(pool, storageModel{
|
||||||
|
BlockNumber: BlockNumber.Uint64() + 3,
|
||||||
|
BlockHash: RandomHash3.String(),
|
||||||
|
LeafKey: AccountLeafKey.String(),
|
||||||
|
StorageLeafKey: StorageLeafKey.String(),
|
||||||
|
StorageCID: RemovedNodeStateCID,
|
||||||
|
Diff: true,
|
||||||
|
Value: []byte{},
|
||||||
|
Removed: true,
|
||||||
|
}))
|
||||||
require.NoError(t, insertContractCode(pool))
|
require.NoError(t, insertContractCode(pool))
|
||||||
|
|
||||||
db, err := statedb.NewStateDatabaseWithPool(pool)
|
db, err := statedb.NewStateDatabaseWithPool(pool)
|
||||||
@ -108,9 +175,33 @@ func TestSuite(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, &Account, acct)
|
require.Equal(t, &Account, acct)
|
||||||
|
|
||||||
|
acct2, err := db.StateAccount(AccountLeafKey, RandomHash)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, &Account, acct2)
|
||||||
|
|
||||||
|
acct3, err := db.StateAccount(AccountLeafKey, RandomHash2)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, &Account, acct3)
|
||||||
|
|
||||||
|
acct4, err := db.StateAccount(AccountLeafKey, RandomHash3)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, (*types.StateAccount)(nil), acct4)
|
||||||
|
|
||||||
val, err := db.StorageValue(AccountLeafKey, StorageLeafKey, BlockHash)
|
val, err := db.StorageValue(AccountLeafKey, StorageLeafKey, BlockHash)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, StoredValueRLP, val)
|
require.Equal(t, StoredValueRLP, val)
|
||||||
|
|
||||||
|
val2, err := db.StorageValue(AccountLeafKey, StorageLeafKey, RandomHash)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, ([]byte)(nil), val2)
|
||||||
|
|
||||||
|
val3, err := db.StorageValue(AccountLeafKey, StorageLeafKey, RandomHash2)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, StoredValueRLP2, val3)
|
||||||
|
|
||||||
|
val4, err := db.StorageValue(AccountLeafKey, StorageLeafKey, RandomHash3)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, ([]byte)(nil), val4)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("StateDB", func(t *testing.T) {
|
t.Run("StateDB", func(t *testing.T) {
|
||||||
@ -162,7 +253,7 @@ func TestSuite(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func insertHeaderCID(db *pgxpool.Pool) error {
|
func insertHeaderCID(db *pgxpool.Pool, blockHash string, blockNumber uint64) error {
|
||||||
sql := `INSERT INTO eth.header_cids (
|
sql := `INSERT INTO eth.header_cids (
|
||||||
block_number,
|
block_number,
|
||||||
block_hash,
|
block_hash,
|
||||||
@ -180,8 +271,8 @@ func insertHeaderCID(db *pgxpool.Pool) error {
|
|||||||
coinbase
|
coinbase
|
||||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)`
|
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)`
|
||||||
_, err := db.Exec(testCtx, sql,
|
_, err := db.Exec(testCtx, sql,
|
||||||
BlockNumber.Uint64(),
|
blockNumber,
|
||||||
BlockHash.String(),
|
blockHash,
|
||||||
BlockParentHash.String(),
|
BlockParentHash.String(),
|
||||||
HeaderCID.String(),
|
HeaderCID.String(),
|
||||||
0, []string{}, 0,
|
0, []string{}, 0,
|
||||||
@ -196,7 +287,20 @@ func insertHeaderCID(db *pgxpool.Pool) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func insertStateCID(db *pgxpool.Pool) error {
|
type stateModel struct {
|
||||||
|
BlockNumber uint64
|
||||||
|
BlockHash string
|
||||||
|
LeafKey string
|
||||||
|
CID string
|
||||||
|
Diff bool
|
||||||
|
Balance uint64
|
||||||
|
Nonce uint64
|
||||||
|
CodeHash string
|
||||||
|
StorageRoot string
|
||||||
|
Removed bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func insertStateCID(db *pgxpool.Pool, cidModel stateModel) error {
|
||||||
sql := `INSERT INTO eth.state_cids (
|
sql := `INSERT INTO eth.state_cids (
|
||||||
block_number,
|
block_number,
|
||||||
header_id,
|
header_id,
|
||||||
@ -210,21 +314,32 @@ func insertStateCID(db *pgxpool.Pool) error {
|
|||||||
removed
|
removed
|
||||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)`
|
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)`
|
||||||
_, err := db.Exec(testCtx, sql,
|
_, err := db.Exec(testCtx, sql,
|
||||||
BlockNumber.Uint64(),
|
cidModel.BlockNumber,
|
||||||
BlockHash.String(),
|
cidModel.BlockHash,
|
||||||
AccountLeafKey.String(),
|
cidModel.LeafKey,
|
||||||
AccountCID.String(),
|
cidModel.CID,
|
||||||
false,
|
cidModel.Diff,
|
||||||
Account.Balance.Uint64(),
|
cidModel.Balance,
|
||||||
Account.Nonce,
|
cidModel.Nonce,
|
||||||
AccountCodeHash.String(),
|
cidModel.CodeHash,
|
||||||
Account.Root.String(),
|
cidModel.StorageRoot,
|
||||||
false,
|
cidModel.Removed,
|
||||||
)
|
)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func insertStorageCID(db *pgxpool.Pool) error {
|
type storageModel struct {
|
||||||
|
BlockNumber uint64
|
||||||
|
BlockHash string
|
||||||
|
LeafKey string
|
||||||
|
StorageLeafKey string
|
||||||
|
StorageCID string
|
||||||
|
Diff bool
|
||||||
|
Value []byte
|
||||||
|
Removed bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func insertStorageCID(db *pgxpool.Pool, cidModel storageModel) error {
|
||||||
sql := `INSERT INTO eth.storage_cids (
|
sql := `INSERT INTO eth.storage_cids (
|
||||||
block_number,
|
block_number,
|
||||||
header_id,
|
header_id,
|
||||||
@ -236,14 +351,14 @@ func insertStorageCID(db *pgxpool.Pool) error {
|
|||||||
removed
|
removed
|
||||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`
|
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`
|
||||||
_, err := db.Exec(testCtx, sql,
|
_, err := db.Exec(testCtx, sql,
|
||||||
BlockNumber.Uint64(),
|
cidModel.BlockNumber,
|
||||||
BlockHash.String(),
|
cidModel.BlockHash,
|
||||||
AccountLeafKey.String(),
|
cidModel.LeafKey,
|
||||||
StorageLeafKey.String(),
|
cidModel.StorageLeafKey,
|
||||||
StorageCID.String(),
|
cidModel.StorageCID,
|
||||||
false,
|
cidModel.Diff,
|
||||||
StoredValueRLP,
|
cidModel.Value,
|
||||||
false,
|
cidModel.Removed,
|
||||||
)
|
)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user