Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit b767531

Browse files
authored
Merge pull request #158 from vulcanize/missed_marked_checked_headers
Fix for issue #146
2 parents 3fff289 + a2d249c commit b767531

39 files changed

+214
-290
lines changed

integration_test/contract_watcher_full_transformer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package integration
1818

1919
import (
2020
"fmt"
21-
"github.com/vulcanize/vulcanizedb/pkg/config"
2221
"math/rand"
2322
"strings"
2423
"time"
@@ -27,6 +26,7 @@ import (
2726
. "github.com/onsi/ginkgo"
2827
. "github.com/onsi/gomega"
2928

29+
"github.com/vulcanize/vulcanizedb/pkg/config"
3030
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/full/transformer"
3131
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/constants"
3232
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/helpers/test_helpers"

integration_test/contract_watcher_header_sync_transformer_test.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ var _ = Describe("contractWatcher headerSync transformer", func() {
4040
var blockChain core.BlockChain
4141
var headerRepository repositories.HeaderRepository
4242
var headerID int64
43-
var ensAddr = strings.ToLower(constants.EnsContractAddress)
44-
var tusdAddr = strings.ToLower(constants.TusdContractAddress)
43+
var ensAddr = strings.ToLower(constants.EnsContractAddress) // 0x314159265dd8dbb310642f98f50c066173c1259b
44+
var tusdAddr = strings.ToLower(constants.TusdContractAddress) // 0x8dd5fbce2f6a956c3022ba3663759011dd51e73e
4545

4646
BeforeEach(func() {
4747
db, blockChain = test_helpers.SetupDBandBC()
@@ -377,6 +377,42 @@ var _ = Describe("contractWatcher headerSync transformer", func() {
377377
Expect(transferLog.Value).To(Equal("2800000000000000000000"))
378378
})
379379

380+
It("Marks header checked for a contract that has no logs at that header", func() {
381+
t := transformer.NewTransformer(test_helpers.ENSandTusdConfig, blockChain, db)
382+
err = t.Init()
383+
Expect(err).ToNot(HaveOccurred())
384+
err = t.Execute()
385+
Expect(err).ToNot(HaveOccurred())
386+
Expect(t.Start).To(Equal(int64(6885702)))
387+
388+
newOwnerLog := test_helpers.HeaderSyncNewOwnerLog{}
389+
err = db.QueryRowx(fmt.Sprintf("SELECT * FROM header_%s.newowner_event", ensAddr)).StructScan(&newOwnerLog)
390+
Expect(err).ToNot(HaveOccurred())
391+
transferLog := test_helpers.HeaderSyncTransferLog{}
392+
err = db.QueryRowx(fmt.Sprintf("SELECT * FROM header_%s.transfer_event", tusdAddr)).StructScan(&transferLog)
393+
Expect(err).ToNot(HaveOccurred())
394+
Expect(transferLog.HeaderID).ToNot(Equal(newOwnerLog.HeaderID))
395+
396+
type checkedHeader struct {
397+
ID int64 `db:"id"`
398+
HeaderID int64 `db:"header_id"`
399+
NewOwner int64 `db:"newowner_0x314159265dd8dbb310642f98f50c066173c1259b"`
400+
Transfer int64 `db:"transfer_0x8dd5fbce2f6a956c3022ba3663759011dd51e73e"`
401+
}
402+
403+
transferCheckedHeader := new(checkedHeader)
404+
err = db.QueryRowx("SELECT * FROM public.checked_headers WHERE header_id = $1", transferLog.HeaderID).StructScan(transferCheckedHeader)
405+
Expect(err).ToNot(HaveOccurred())
406+
Expect(transferCheckedHeader.Transfer).To(Equal(int64(1)))
407+
Expect(transferCheckedHeader.NewOwner).To(Equal(int64(1)))
408+
409+
newOwnerCheckedHeader := new(checkedHeader)
410+
err = db.QueryRowx("SELECT * FROM public.checked_headers WHERE header_id = $1", newOwnerLog.HeaderID).StructScan(newOwnerCheckedHeader)
411+
Expect(err).ToNot(HaveOccurred())
412+
Expect(newOwnerCheckedHeader.NewOwner).To(Equal(int64(1)))
413+
Expect(newOwnerCheckedHeader.Transfer).To(Equal(int64(1)))
414+
})
415+
380416
It("Keeps track of contract-related hashes and addresses while transforming event data if they need to be used for later method polling", func() {
381417
var testConf config.ContractConfig
382418
testConf = test_helpers.ENSandTusdConfig

libraries/shared/repository/repository.go

Lines changed: 0 additions & 27 deletions
This file was deleted.

libraries/shared/repository/repository_test.go

Lines changed: 0 additions & 67 deletions
This file was deleted.

pkg/contract_watcher/full/converter/converter.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package converter
1818

1919
import (
20-
"errors"
2120
"fmt"
2221
"math/big"
2322
"strconv"
@@ -32,22 +31,24 @@ import (
3231
"github.com/vulcanize/vulcanizedb/pkg/core"
3332
)
3433

35-
// Converter is used to convert watched event logs to
34+
// ConverterInterface is used to convert watched event logs to
3635
// custom logs containing event input name => value maps
3736
type ConverterInterface interface {
3837
Convert(watchedEvent core.WatchedEvent, event types.Event) (*types.Log, error)
3938
Update(info *contract.Contract)
4039
}
4140

41+
// Converter is the underlying struct for the ConverterInterface
4242
type Converter struct {
4343
ContractInfo *contract.Contract
4444
}
4545

46+
// Update configures the converter for a specific contract
4647
func (c *Converter) Update(info *contract.Contract) {
4748
c.ContractInfo = info
4849
}
4950

50-
// Convert the given watched event log into a types.Log for the given event
51+
// Convert converts the given watched event log into a types.Log for the given event
5152
func (c *Converter) Convert(watchedEvent core.WatchedEvent, event types.Event) (*types.Log, error) {
5253
boundContract := bind.NewBoundContract(common.HexToAddress(c.ContractInfo.Address), c.ContractInfo.ParsedAbi, nil, nil, nil)
5354
values := make(map[string]interface{})
@@ -88,14 +89,14 @@ func (c *Converter) Convert(watchedEvent core.WatchedEvent, event types.Event) (
8889
b := input.(byte)
8990
strValues[fieldName] = string(b)
9091
default:
91-
return nil, errors.New(fmt.Sprintf("error: unhandled abi type %T", input))
92+
return nil, fmt.Errorf("error: unhandled abi type %T", input)
9293
}
9394
}
9495

9596
// Only hold onto logs that pass our address filter, if any
9697
if c.ContractInfo.PassesEventFilter(strValues) {
9798
eventLog := &types.Log{
98-
Id: watchedEvent.LogID,
99+
ID: watchedEvent.LogID,
99100
Values: strValues,
100101
Block: watchedEvent.BlockNumber,
101102
Tx: watchedEvent.TxHash,

pkg/contract_watcher/full/retriever/block_retriever.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ package retriever
1818

1919
import (
2020
"database/sql"
21+
2122
"github.com/vulcanize/vulcanizedb/libraries/shared/repository"
2223
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
2324
)
2425

25-
// Block retriever is used to retrieve the first block for a given contract and the most recent block
26+
// BlockRetriever is used to retrieve the first block for a given contract and the most recent block
2627
// It requires a vDB synced database with blocks, transactions, receipts, and logs
2728
type BlockRetriever interface {
2829
RetrieveFirstBlock(contractAddr string) (int64, error)
@@ -33,13 +34,15 @@ type blockRetriever struct {
3334
db *postgres.DB
3435
}
3536

36-
func NewBlockRetriever(db *postgres.DB) (r *blockRetriever) {
37+
// NewBlockRetriever returns a new BlockRetriever
38+
func NewBlockRetriever(db *postgres.DB) BlockRetriever {
3739
return &blockRetriever{
3840
db: db,
3941
}
4042
}
4143

42-
// Try both methods of finding the first block, with the receipt method taking precedence
44+
// RetrieveFirstBlock fetches the block number for the earliest block in the db
45+
// Tries both methods of finding the first block, with the receipt method taking precedence
4346
func (r *blockRetriever) RetrieveFirstBlock(contractAddr string) (int64, error) {
4447
i, err := r.retrieveFirstBlockFromReceipts(contractAddr)
4548
if err != nil {
@@ -55,7 +58,7 @@ func (r *blockRetriever) RetrieveFirstBlock(contractAddr string) (int64, error)
5558
// For some contracts the contract creation transaction receipt doesn't have the contract address so this doesn't work (e.g. Sai)
5659
func (r *blockRetriever) retrieveFirstBlockFromReceipts(contractAddr string) (int64, error) {
5760
var firstBlock int64
58-
addressId, getAddressErr := repository.GetOrCreateAddress(r.db, contractAddr)
61+
addressID, getAddressErr := repository.GetOrCreateAddress(r.db, contractAddr)
5962
if getAddressErr != nil {
6063
return firstBlock, getAddressErr
6164
}
@@ -66,7 +69,7 @@ func (r *blockRetriever) retrieveFirstBlockFromReceipts(contractAddr string) (in
6669
WHERE contract_address_id = $1
6770
ORDER BY block_id ASC
6871
LIMIT 1)`,
69-
addressId,
72+
addressID,
7073
)
7174

7275
return firstBlock, err
@@ -84,7 +87,7 @@ func (r *blockRetriever) retrieveFirstBlockFromLogs(contractAddr string) (int64,
8487
return int64(firstBlock), err
8588
}
8689

87-
// Method to retrieve the most recent block in vDB
90+
// RetrieveMostRecentBlock retrieves the most recent block number in vDB
8891
func (r *blockRetriever) RetrieveMostRecentBlock() (int64, error) {
8992
var lastBlock int64
9093
err := r.db.Get(

pkg/contract_watcher/full/retriever/block_retriever_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
package retriever_test
1818

1919
import (
20+
"strings"
21+
2022
"github.com/ethereum/go-ethereum/core/types"
2123
. "github.com/onsi/ginkgo"
2224
. "github.com/onsi/gomega"
23-
"strings"
2425

2526
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/full/retriever"
2627
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/constants"

pkg/contract_watcher/full/retriever/retriever_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
package retriever_test
1818

1919
import (
20-
"github.com/sirupsen/logrus"
2120
"io/ioutil"
2221
"testing"
2322

2423
. "github.com/onsi/ginkgo"
2524
. "github.com/onsi/gomega"
25+
"github.com/sirupsen/logrus"
2626
)
2727

2828
func TestRetriever(t *testing.T) {

pkg/contract_watcher/full/transformer/transformer.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
3636
)
3737

38+
// Transformer is the top level struct for transforming watched contract data
3839
// Requires a fully synced vDB and a running eth node (or infura)
3940
type Transformer struct {
4041
// Database interfaces
@@ -60,7 +61,7 @@ type Transformer struct {
6061
LastBlock int64
6162
}
6263

63-
// Transformer takes in config for blockchain, database, and network id
64+
// NewTransformer takes in contract config, blockchain, and database, and returns a new Transformer
6465
func NewTransformer(con config.ContractConfig, BC core.BlockChain, DB *postgres.DB) *Transformer {
6566
return &Transformer{
6667
Poller: poller.NewPoller(BC, DB, types.FullSync),
@@ -75,6 +76,7 @@ func NewTransformer(con config.ContractConfig, BC core.BlockChain, DB *postgres.
7576
}
7677
}
7778

79+
// Init initializes the transformer
7880
// Use after creating and setting transformer
7981
// Loops over all of the addr => filter sets
8082
// Uses parser to pull event info from abi
@@ -167,6 +169,7 @@ func (tr *Transformer) Init() error {
167169
return nil
168170
}
169171

172+
// Execute runs the transformation processes
170173
// Iterates through stored, initialized contract objects
171174
// Iterates through contract's event filters, grabbing watched event logs
172175
// Uses converter to convert logs into custom log type
@@ -227,6 +230,7 @@ func (tr *Transformer) Execute() error {
227230
return nil
228231
}
229232

233+
// GetConfig returns the transformers config; satisfies the transformer interface
230234
func (tr *Transformer) GetConfig() config.ContractConfig {
231235
return tr.Config
232236
}

pkg/contract_watcher/full/transformer/transformer_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
package transformer_test
1818

1919
import (
20-
"github.com/sirupsen/logrus"
2120
"io/ioutil"
2221
"testing"
2322

2423
. "github.com/onsi/ginkgo"
2524
. "github.com/onsi/gomega"
25+
"github.com/sirupsen/logrus"
2626
)
2727

2828
func TestTransformer(t *testing.T) {

0 commit comments

Comments
 (0)