ipld-eth-db-validator/validator_test/validator_test.go

135 lines
4.0 KiB
Go
Raw Normal View History

package validator_test_test
2022-01-18 17:14:38 +00:00
import (
"context"
"math/big"
"github.com/cerc-io/ipld-eth-server/v4/pkg/eth/test_helpers"
"github.com/cerc-io/ipld-eth-server/v4/pkg/shared"
2022-01-18 17:14:38 +00:00
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rpc"
2022-01-18 17:14:38 +00:00
"github.com/ethereum/go-ethereum/statediff"
sdtypes "github.com/ethereum/go-ethereum/statediff/types"
2022-05-06 11:21:11 +00:00
"github.com/jmoiron/sqlx"
2022-01-18 17:14:38 +00:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
2022-05-06 11:21:11 +00:00
"github.com/vulcanize/ipld-eth-db-validator/pkg/validator"
"github.com/vulcanize/ipld-eth-db-validator/validator_test"
2022-01-18 17:14:38 +00:00
)
const (
chainLength = 20
blockHeight = 1
trail = 2
2022-01-18 17:14:38 +00:00
)
var _ = Describe("eth state reading tests", func() {
var (
blocks []*types.Block
receipts []types.Receipts
chain *core.BlockChain
2022-05-06 11:21:11 +00:00
db *sqlx.DB
2022-05-17 03:22:19 +00:00
chainConfig = validator.TestChainConfig
2022-01-18 17:14:38 +00:00
mockTD = big.NewInt(1337)
)
It("test init", func() {
2022-05-18 12:05:30 +00:00
db = shared.SetupDB()
transformer := shared.SetupTestStateDiffIndexer(context.Background(), chainConfig, validator_test.Genesis.Hash())
2022-01-18 17:14:38 +00:00
// make the test blockchain (and state)
2022-05-17 03:22:19 +00:00
blocks, receipts, chain = validator_test.MakeChain(chainLength, validator_test.Genesis, validator_test.TestChainGen)
2022-01-18 17:14:38 +00:00
params := statediff.Params{
IntermediateStateNodes: true,
IntermediateStorageNodes: true,
}
// iterate over the blocks, generating statediff payloads, and transforming the data into Postgres
builder := statediff.NewBuilder(chain.StateCache())
for i, block := range blocks {
var args statediff.Args
var rcts types.Receipts
if i == 0 {
args = statediff.Args{
OldStateRoot: common.Hash{},
NewStateRoot: block.Root(),
BlockNumber: block.Number(),
BlockHash: block.Hash(),
}
} else {
args = statediff.Args{
OldStateRoot: blocks[i-1].Root(),
NewStateRoot: block.Root(),
BlockNumber: block.Number(),
BlockHash: block.Hash(),
}
rcts = receipts[i-1]
}
diff, err := builder.BuildStateDiffObject(args, params)
Expect(err).ToNot(HaveOccurred())
tx, err := transformer.PushBlock(block, rcts, mockTD)
Expect(err).ToNot(HaveOccurred())
for _, node := range diff.Nodes {
2022-05-06 11:21:11 +00:00
err := transformer.PushStateNode(tx, node, block.Hash().String())
2022-01-18 17:14:38 +00:00
Expect(err).ToNot(HaveOccurred())
}
2022-05-06 11:21:11 +00:00
err = tx.Submit(err)
2022-01-18 17:14:38 +00:00
Expect(err).ToNot(HaveOccurred())
}
// Insert some non-canonical data into the database so that we test our ability to discern canonicity
2022-05-18 12:05:30 +00:00
indexAndPublisher := shared.SetupTestStateDiffIndexer(context.Background(), chainConfig, validator_test.Genesis.Hash())
2022-01-18 17:14:38 +00:00
tx, err := indexAndPublisher.PushBlock(test_helpers.MockBlock, test_helpers.MockReceipts, test_helpers.MockBlock.Difficulty())
Expect(err).ToNot(HaveOccurred())
2022-05-06 11:21:11 +00:00
err = tx.Submit(err)
2022-01-18 17:14:38 +00:00
Expect(err).ToNot(HaveOccurred())
// The non-canonical header has a child
tx, err = indexAndPublisher.PushBlock(test_helpers.MockChild, test_helpers.MockReceipts, test_helpers.MockChild.Difficulty())
Expect(err).ToNot(HaveOccurred())
hash := sdtypes.CodeAndCodeHash{
Hash: test_helpers.CodeHash,
Code: test_helpers.ContractCode,
}
err = indexAndPublisher.PushCodeAndCodeHash(tx, hash)
Expect(err).ToNot(HaveOccurred())
2022-05-06 11:21:11 +00:00
err = tx.Submit(err)
2022-01-18 17:14:38 +00:00
Expect(err).ToNot(HaveOccurred())
})
defer It("test teardown", func() {
2022-05-18 12:05:30 +00:00
shared.TearDownDB(db)
2022-01-18 17:14:38 +00:00
chain.Stop()
})
Describe("state_validation", func() {
It("Validator", func() {
api, err := validator.EthAPI(context.Background(), db, validator.TestChainConfig)
2022-01-18 17:14:38 +00:00
Expect(err).ToNot(HaveOccurred())
for i := uint64(blockHeight); i <= chainLength-trail; i++ {
blockToBeValidated, err := api.B.BlockByNumber(context.Background(), rpc.BlockNumber(i))
Expect(err).ToNot(HaveOccurred())
Expect(blockToBeValidated).ToNot(BeNil())
err = validator.ValidateBlock(blockToBeValidated, api.B, i)
Expect(err).ToNot(HaveOccurred())
err = validator.ValidateReferentialIntegrity(db, i)
Expect(err).ToNot(HaveOccurred())
}
2022-01-18 17:14:38 +00:00
})
})
})