Skip to content

Commit

Permalink
validate address
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc committed May 30, 2023
1 parent 790c4f5 commit 068b301
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
7 changes: 5 additions & 2 deletions blockindex/contractstaking/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ type (
)

// NewContractStakingIndexer creates a new contract staking indexer
func NewContractStakingIndexer(kvStore db.KVStore, contractAddr string, contractDeployHeight uint64) *Indexer {
func NewContractStakingIndexer(kvStore db.KVStore, contractAddr string, contractDeployHeight uint64) (*Indexer, error) {
if _, err := address.FromString(contractAddr); err != nil {
return nil, errors.Wrapf(err, "invalid contract address %s", contractAddr)
}
return &Indexer{
kvstore: kvStore,
cache: newContractStakingCache(contractAddr),
contractAddress: contractAddr,
contractDeployHeight: contractDeployHeight,
}
}, nil
}

// Start starts the indexer
Expand Down
18 changes: 12 additions & 6 deletions blockindex/contractstaking/indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func TestContractStakingIndexerLoadCache(t *testing.T) {
cfg := db.DefaultConfig
cfg.DbPath = testDBPath
kvStore := db.NewBoltDB(cfg)
indexer := NewContractStakingIndexer(kvStore, _testStakingContractAddress, 0)
indexer, err := NewContractStakingIndexer(kvStore, _testStakingContractAddress, 0)
r.NoError(err)
r.NoError(indexer.Start(context.Background()))

// create a stake
Expand All @@ -51,7 +52,8 @@ func TestContractStakingIndexerLoadCache(t *testing.T) {
r.NoError(indexer.Stop(context.Background()))

// load cache from db
newIndexer := NewContractStakingIndexer(db.NewBoltDB(cfg), _testStakingContractAddress, 0)
newIndexer, err := NewContractStakingIndexer(db.NewBoltDB(cfg), _testStakingContractAddress, 0)
r.NoError(err)
r.NoError(newIndexer.Start(context.Background()))

// check cache
Expand All @@ -76,7 +78,8 @@ func TestContractStakingIndexerDirty(t *testing.T) {
cfg := db.DefaultConfig
cfg.DbPath = testDBPath
kvStore := db.NewBoltDB(cfg)
indexer := NewContractStakingIndexer(kvStore, _testStakingContractAddress, 0)
indexer, err := NewContractStakingIndexer(kvStore, _testStakingContractAddress, 0)
r.NoError(err)
r.NoError(indexer.Start(context.Background()))

// before commit dirty, the cache should be empty
Expand All @@ -103,7 +106,8 @@ func TestContractStakingIndexerThreadSafe(t *testing.T) {
cfg := db.DefaultConfig
cfg.DbPath = testDBPath
kvStore := db.NewBoltDB(cfg)
indexer := NewContractStakingIndexer(kvStore, _testStakingContractAddress, 0)
indexer, err := NewContractStakingIndexer(kvStore, _testStakingContractAddress, 0)
r.NoError(err)
r.NoError(indexer.Start(context.Background()))

wait := sync.WaitGroup{}
Expand Down Expand Up @@ -156,7 +160,8 @@ func TestContractStakingIndexerBucketType(t *testing.T) {
cfg := db.DefaultConfig
cfg.DbPath = testDBPath
kvStore := db.NewBoltDB(cfg)
indexer := NewContractStakingIndexer(kvStore, _testStakingContractAddress, 0)
indexer, err := NewContractStakingIndexer(kvStore, _testStakingContractAddress, 0)
r.NoError(err)
r.NoError(indexer.Start(context.Background()))

// activate
Expand Down Expand Up @@ -238,7 +243,8 @@ func TestContractStakingIndexerBucketInfo(t *testing.T) {
cfg := db.DefaultConfig
cfg.DbPath = testDBPath
kvStore := db.NewBoltDB(cfg)
indexer := NewContractStakingIndexer(kvStore, _testStakingContractAddress, 0)
indexer, err := NewContractStakingIndexer(kvStore, _testStakingContractAddress, 0)
r.NoError(err)
r.NoError(indexer.Start(context.Background()))

// init bucket type
Expand Down
6 changes: 5 additions & 1 deletion chainservice/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,11 @@ func (builder *Builder) buildContractStakingIndexer(forTest bool) error {
} else {
dbConfig := builder.cfg.DB
dbConfig.DbPath = builder.cfg.Chain.ContractStakingIndexDBPath
builder.cs.contractStakingIndexer = contractstaking.NewContractStakingIndexer(db.NewBoltDB(dbConfig), builder.cfg.Genesis.SystemStakingContractAddress, builder.cfg.Genesis.SystemStakingContractHeight)
indexer, err := contractstaking.NewContractStakingIndexer(db.NewBoltDB(dbConfig), builder.cfg.Genesis.SystemStakingContractAddress, builder.cfg.Genesis.SystemStakingContractHeight)
if err != nil {
return err
}
builder.cs.contractStakingIndexer = indexer
}
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion e2etest/contract_staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1706,7 +1706,8 @@ func prepareContractStakingBlockchain(ctx context.Context, cfg config.Config, r
r.NoError(err)
cc := cfg.DB
cc.DbPath = testContractStakeIndexerPath
contractStakeIndexer := contractstaking.NewContractStakingIndexer(db.NewBoltDB(cc), _stakingContractAddress, 0)
contractStakeIndexer, err := contractstaking.NewContractStakingIndexer(db.NewBoltDB(cc), _stakingContractAddress, 0)
r.NoError(err)
// create BlockDAO
dao := blockdao.NewBlockDAOInMemForTest([]blockdao.BlockIndexer{sf, indexer, contractStakeIndexer})
r.NotNil(dao)
Expand Down

0 comments on commit 068b301

Please sign in to comment.