Skip to content

Commit

Permalink
[staking] validate address of contract indexer (#3874)
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc committed Jun 6, 2023
1 parent 59ad616 commit fc67863
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
10 changes: 8 additions & 2 deletions blockindex/contractstaking/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,19 @@ 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 kvStore == nil {
return nil, errors.New("kv store is nil")
}
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
43 changes: 37 additions & 6 deletions blockindex/contractstaking/indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,31 @@ const (
_testStakingContractAddress = "io19ys8f4uhwms6lq6ulexr5fwht9gsjes8mvuugd"
)

func TestNewContractStakingIndexer(t *testing.T) {
r := require.New(t)

t.Run("kvStore is nil", func(t *testing.T) {
_, err := NewContractStakingIndexer(nil, "io19ys8f4uhwms6lq6ulexr5fwht9gsjes8mvuugd", 0)
r.Error(err)
r.Contains(err.Error(), "kv store is nil")
})

t.Run("invalid contract address", func(t *testing.T) {
kvStore := db.NewMemKVStore()
_, err := NewContractStakingIndexer(kvStore, "invalid address", 0)
r.Error(err)
r.Contains(err.Error(), "invalid contract address")
})

t.Run("valid input", func(t *testing.T) {
contractAddr, err := address.FromString("io19ys8f4uhwms6lq6ulexr5fwht9gsjes8mvuugd")
r.NoError(err)
indexer, err := NewContractStakingIndexer(db.NewMemKVStore(), contractAddr.String(), 0)
r.NoError(err)
r.NotNil(indexer)
})
}

func TestContractStakingIndexerLoadCache(t *testing.T) {
r := require.New(t)
testDBPath, err := testutil.PathOfTempFile("staking.db")
Expand All @@ -34,7 +59,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 +77,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 +103,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 +131,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 +185,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 +268,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 @@ -298,7 +298,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 fc67863

Please sign in to comment.