Skip to content

Commit

Permalink
fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
millken committed Apr 19, 2023
1 parent e050ec6 commit 3fb9a0f
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 88 deletions.
2 changes: 0 additions & 2 deletions blockchain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ type (
StakingIndexDBPath string `yaml:"stakingIndexDBPath"`
SGDIndexDBPath string `yaml:"sgdIndexDBPath"`
SGDIndexCacheSize int `yaml:"sgdIndexCacheSize"`
SGDPercentage uint64 `yaml:"sgdPercentage"`
ID uint32 `yaml:"id"`
EVMNetworkID uint32 `yaml:"evmNetworkID"`
Address string `yaml:"address"`
Expand Down Expand Up @@ -88,7 +87,6 @@ var (
StakingIndexDBPath: "/var/data/staking.index.db",
SGDIndexDBPath: "/var/data/sgd.index.db",
SGDIndexCacheSize: 1000,
SGDPercentage: 20,
ID: 1,
EVMNetworkID: 4689,
Address: "",
Expand Down
6 changes: 3 additions & 3 deletions blockindex/indexpb/index.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions blockindex/indexpb/index.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ message SGDIndex {
string contract = 1;
string deployer = 2;
string receiver = 3;
int64 createTime = 4;
uint64 createTime = 4;
int32 callTimes = 5;
}
}
100 changes: 33 additions & 67 deletions blockindex/sgd.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ import (
)

const (
_sgdBucket = "sgd"
_sgdToHeightNS = "hh"
_sgdActCreate sgdActType = iota
_sgdActCall
_sgdBucket = "sg"
_sgdToHeightNS = "hh"
)

var _sgdCurrentHeight = []byte("currentHeight")
Expand All @@ -45,33 +43,37 @@ type (
}

sgdRegistry struct {
kvStore db.KVStore
kvCache cache.LRUCache
percentage uint64
kvStore db.KVStore
kvCache cache.LRUCache
}

sgdActType int

sgdAct struct {
actType sgdActType
sender address.Address
contract string
createTime time.Time
}
)

func newSgdIndex(contract, deployer string, createTime uint64) *indexpb.SGDIndex {
return &indexpb.SGDIndex{
Contract: contract,
CreateTime: createTime,
Deployer: deployer,
}
}

// NewSGDRegistry creates a new SGDIndexer
func NewSGDRegistry(kv db.KVStore, kvCache cache.LRUCache, percentage uint64) SGDIndexer {
func NewSGDRegistry(kv db.KVStore, cacheSize int) SGDIndexer {
if kv == nil {
panic("nil kvstore")
}
if percentage > 100 {
panic("percentage should be less than 100")
kvCache := cache.NewDummyLruCache()
if cacheSize > 0 {
kvCache = cache.NewThreadSafeLruCache(cacheSize)
}
return &sgdRegistry{
kvStore: kv,
percentage: percentage,
kvCache: kvCache,
kvStore: kv,
kvCache: kvCache,
}
}

Expand Down Expand Up @@ -101,15 +103,10 @@ func (sgd *sgdRegistry) Height() (uint64, error) {
// PutBlock puts a block into SGDIndexer
func (sgd *sgdRegistry) PutBlock(ctx context.Context, blk *block.Block) error {
var (
index *indexpb.SGDIndex
r *action.Receipt
actType sgdActType
contract string
ok bool
index *indexpb.SGDIndex
r *action.Receipt
ok bool
)
if blk == nil {
return errors.New("empty block")
}
b := batch.NewBatch()
receipts := getReceiptsFromBlock(blk)
for _, selp := range blk.Actions {
Expand All @@ -124,22 +121,16 @@ func (sgd *sgdRegistry) PutBlock(ctx context.Context, blk *block.Block) error {
if !ok || r.Status != uint64(iotextypes.ReceiptStatus_Success) {
continue
}
if r.ContractAddress != "" {
actType = _sgdActCreate
contract = r.ContractAddress
} else {
actType = _sgdActCall
contract = act.Destination()
}
sender, _ := address.FromBytes(selp.SrcPubkey().Hash())
sgdAct := sgdAct{
actType: actType,
sender: sender,
contract: contract,
createTime: blk.Header.Timestamp(),
}
if index, err = sgd.actToIndex(sgdAct); err != nil {
return err
if len(r.ContractAddress) > 0 {
// contract creation
index = newSgdIndex(r.ContractAddress, sender.String(), uint64(blk.Header.Timestamp().Unix()))
} else {
index, err = sgd.GetSGDIndex(act.Destination())
if err != nil {
return err
}
index.CallTimes++
}
if err := sgd.putIndex(b, index); err != nil {
return err
Expand All @@ -151,32 +142,6 @@ func (sgd *sgdRegistry) PutBlock(ctx context.Context, blk *block.Block) error {
return sgd.kvStore.WriteBatch(b)
}

func (sgd *sgdRegistry) actToIndex(sgdAct sgdAct) (*indexpb.SGDIndex, error) {
var (
sgdIndex *indexpb.SGDIndex
err error
)
switch sgdAct.actType {
case _sgdActCreate:
sgdIndex = &indexpb.SGDIndex{
Contract: sgdAct.contract,
Deployer: sgdAct.sender.String(),
CreateTime: sgdAct.createTime.Unix(),
}
case _sgdActCall:
sgdIndex, err = sgd.GetSGDIndex(sgdAct.contract)
if err != nil {
//make sure running when the contract is not exist
sgdIndex = &indexpb.SGDIndex{
Contract: sgdAct.contract,
}
}
sgdIndex.CallTimes++
}
return sgdIndex, nil

}

func (sgd *sgdRegistry) putIndex(b batch.KVStoreBatch, sgdIndex *indexpb.SGDIndex) error {
sgdIndexBytes, err := proto.Marshal(sgdIndex)
if err != nil {
Expand All @@ -201,9 +166,10 @@ func (sgd *sgdRegistry) CheckContract(contract string) (address.Address, uint64,
addr, err := address.FromString(sgdIndex.Receiver)
if err != nil {
// if the receiver is no set or invalid
return nil, sgd.percentage, true
return nil, 0, true
}
return addr, sgd.percentage, true
percentage := uint64(20)
return addr, percentage, true
}

// GetSGDIndex returns the SGDIndex of the contract
Expand Down
3 changes: 0 additions & 3 deletions blockindex/sgd_dummy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,4 @@ func TestSGDDummy(t *testing.T) {
if _, _, ok := r.CheckContract(""); ok {
t.Error("should return false")
}
if _, err := r.GetSGDIndex(""); err != nil {
t.Error(err)
}
}
12 changes: 4 additions & 8 deletions blockindex/sgd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"testing"
"time"

"github.com/iotexproject/go-pkgs/cache"
"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/blockchain/block"
"github.com/iotexproject/iotex-core/db"
Expand All @@ -18,17 +17,14 @@ import (
func TestSGDRegistry(t *testing.T) {
require := require.New(t)
kv := db.NewMemKVStore()
indexer := NewSGDRegistry(kv, cache.NewDummyLruCache(), 10)
indexer := NewSGDRegistry(kv, 0)
ctx := context.Background()
require.NoError(indexer.Start(ctx))
defer func() {
require.NoError(indexer.Stop(ctx))
}()

fakeTime := time.Now()
t.Run("put nil block", func(t *testing.T) {
require.ErrorContains(indexer.PutBlock(ctx, nil), "empty")
})

t.Run("put block with no execution", func(t *testing.T) {
tsf1, err := action.SignedTransfer(identityset.Address(28).String(), identityset.PrivateKey(28), 1, big.NewInt(int64(1)), nil, testutil.TestGasLimit, big.NewInt(0))
Expand Down Expand Up @@ -70,7 +66,7 @@ func TestSGDRegistry(t *testing.T) {
idx, err := indexer.GetSGDIndex(identityset.Address(31).String())
require.NoError(err)
require.NotNil(idx)
require.Equal(fakeTime.Unix(), idx.CreateTime)
require.Equal(uint64(fakeTime.Unix()), idx.CreateTime)
require.Equal(identityset.Address(28).String(), idx.Deployer)
require.Equal(identityset.Address(31).String(), idx.Contract)
require.Equal("", idx.Receiver)
Expand Down Expand Up @@ -100,7 +96,7 @@ func TestSGDRegistry(t *testing.T) {
idx, err := indexer.GetSGDIndex(identityset.Address(31).String())
require.NoError(err)
require.NotNil(idx)
require.Equal(fakeTime.Unix(), idx.CreateTime)
require.Equal(uint64(fakeTime.Unix()), idx.CreateTime)
require.Equal(identityset.Address(28).String(), idx.Deployer)
require.Equal(identityset.Address(31).String(), idx.Contract)
require.Equal("", idx.Receiver)
Expand All @@ -111,7 +107,7 @@ func TestSGDRegistry(t *testing.T) {
addr, percentage, ok := indexer.CheckContract(identityset.Address(31).String())
require.True(ok)
require.Equal(nil, addr)
require.Equal(uint64(10), percentage)
require.Equal(uint64(0), percentage)
})

t.Run("test DeleteTipBlock", func(t *testing.T) {
Expand Down
5 changes: 2 additions & 3 deletions chainservice/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"math/big"
"time"

"github.com/iotexproject/go-pkgs/cache"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
Expand Down Expand Up @@ -280,8 +279,8 @@ func (builder *Builder) buildSGDRegistry(forTest bool) error {
dbConfig.DbPath = builder.cfg.Chain.SGDIndexDBPath
builder.cs.sgdRegistry = blockindex.NewSGDRegistry(
db.NewBoltDB(dbConfig),
cache.NewThreadSafeLruCache(builder.cfg.Chain.SGDIndexCacheSize),
builder.cfg.Chain.SGDPercentage)
builder.cfg.Chain.SGDIndexCacheSize,
)
}
return nil
}
Expand Down

0 comments on commit 3fb9a0f

Please sign in to comment.