(VDB-68) Verify log block hash matches header block hash

- Delete header on conflict to prompt data refresh (cascade deletes all
  data associated with that block)
- Derive header hash from rpc payload rather than computing it from data
  (prevents hash mismatch between blockchain and cache)
This commit is contained in:
Rob Mulholand
2018-11-13 14:51:39 -06:00
parent 9d26c174d4
commit 82fd73ba3f
95 changed files with 557 additions and 298 deletions
@@ -15,6 +15,7 @@
package shared_behaviors
import (
"github.com/ethereum/go-ethereum/common"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/core"
@@ -23,6 +24,7 @@ import (
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
"github.com/vulcanize/vulcanizedb/pkg/fakes"
"github.com/vulcanize/vulcanizedb/pkg/transformers/factories"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
"github.com/vulcanize/vulcanizedb/test_config"
"math/rand"
@@ -101,6 +103,35 @@ func SharedRepositoryCreateBehaviors(inputs *CreateBehaviorInputs) {
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
})
Describe("when log's block hash does not match associated header's block hash", func() {
var (
differentHeaderID int64
err error
)
BeforeEach(func() {
differentHeader := fakes.FakeHeader
differentHeader.BlockNumber = fakes.FakeHeader.BlockNumber + 1
differentHeader.Hash = common.BytesToHash(append(fakes.FakeHash.Bytes(), 1)).String()
differentHeaderID, err = headerRepository.CreateOrUpdateHeader(differentHeader)
Expect(err).NotTo(HaveOccurred())
err = repository.Create(differentHeaderID, []interface{}{logEventModel})
})
It("returns error", func() {
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(shared.ErrHeaderMismatch))
})
It("removes header", func() {
var headerIDs []int64
err = db.Select(&headerIDs, `SELECT id FROM public.headers`)
Expect(err).NotTo(HaveOccurred())
Expect(headerIDs).NotTo(ContainElement(differentHeaderID))
})
})
It("allows for multiple log events of the same type in one transaction if they have different log indexes", func() {
err = repository.Create(headerID, []interface{}{logEventModel})
Expect(err).NotTo(HaveOccurred())