Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iip-15]sgdRegistry implementation #3845

Merged
merged 33 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
68d964a
[iip-15]sgdRegistry implementation
millken Apr 14, 2023
e050ec6
fix server tests
millken Apr 14, 2023
3fb9a0f
fix comments
millken Apr 19, 2023
679d327
fix comments
millken Apr 19, 2023
b11d20d
remove sgdAct
millken Apr 19, 2023
51fbc45
Merge branch 'master' into iip15-sgd
millken May 8, 2023
cb1fb4d
Revert "remove sgdAct"
millken May 8, 2023
2b3a232
Revert "fix comments"
millken May 8, 2023
57a9973
Revert "fix comments"
millken May 8, 2023
ec02883
Revert "fix server tests"
millken May 8, 2023
bf8079a
Revert "[iip-15]sgdRegistry implementation"
millken May 8, 2023
adc3ec6
init sgd registry
millken May 8, 2023
1b4955d
implement sgdRegistry, added e2etest
millken May 9, 2023
7808510
Merge branch 'master' into iip15-sgd
millken May 9, 2023
4f14ef6
fix comments
millken May 10, 2023
7d64233
fix comments
millken May 10, 2023
779e6db
monitor contract events
millken May 16, 2023
ef749eb
remove unused file
millken May 16, 2023
dd8372a
Merge branch 'master' into iip15-sgd
millken May 16, 2023
e7c3abd
fix merged code
millken May 16, 2023
1951bae
fix tests
millken May 17, 2023
1e5f441
optimized code
millken May 17, 2023
6ad008d
fix tests
millken May 17, 2023
ad8d2d8
fix comments
millken May 17, 2023
4fc5e39
fix comments
millken May 18, 2023
ad49288
fix comments
millken May 18, 2023
9d6149d
add FetchContracts interface to sgdRegistry
millken May 19, 2023
71f9386
fix comments
millken May 23, 2023
e17fbce
fix comments
millken May 24, 2023
d4001c8
update SGDRegistry, improve checkIndex performance
millken May 30, 2023
203d6a1
fix ci flow
millken May 30, 2023
e6152e9
fix ci flow
millken May 30, 2023
0087a81
fix yaml var name
millken May 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 36 additions & 35 deletions blockchain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,25 @@ import (
type (
// Config is the config struct for blockchain package
Config struct {
ChainDBPath string `yaml:"chainDBPath"`
TrieDBPatchFile string `yaml:"trieDBPatchFile"`
TrieDBPath string `yaml:"trieDBPath"`
StakingPatchDir string `yaml:"stakingPatchDir"`
IndexDBPath string `yaml:"indexDBPath"`
BloomfilterIndexDBPath string `yaml:"bloomfilterIndexDBPath"`
CandidateIndexDBPath string `yaml:"candidateIndexDBPath"`
StakingIndexDBPath string `yaml:"stakingIndexDBPath"`
SGDIndexDBPath string `yaml:"sgdIndexDBPath"`
SGDContract string `yaml:"sgdContract"`
ID uint32 `yaml:"id"`
EVMNetworkID uint32 `yaml:"evmNetworkID"`
Address string `yaml:"address"`
ProducerPrivKey string `yaml:"producerPrivKey"`
ProducerPrivKeySchema string `yaml:"producerPrivKeySchema"`
SignatureScheme []string `yaml:"signatureScheme"`
EmptyGenesis bool `yaml:"emptyGenesis"`
GravityChainDB db.Config `yaml:"gravityChainDB"`
Committee committee.Config `yaml:"committee"`
ChainDBPath string `yaml:"chainDBPath"`
TrieDBPatchFile string `yaml:"trieDBPatchFile"`
TrieDBPath string `yaml:"trieDBPath"`
StakingPatchDir string `yaml:"stakingPatchDir"`
IndexDBPath string `yaml:"indexDBPath"`
BloomfilterIndexDBPath string `yaml:"bloomfilterIndexDBPath"`
CandidateIndexDBPath string `yaml:"candidateIndexDBPath"`
StakingIndexDBPath string `yaml:"stakingIndexDBPath"`
SGDIndexDBPath string `yaml:"sgdIndexDBPath"`
ContractStakingIndexDBPath string `yaml:"contractStakingIndexDBPath"`
ID uint32 `yaml:"id"`
EVMNetworkID uint32 `yaml:"evmNetworkID"`
Address string `yaml:"address"`
ProducerPrivKey string `yaml:"producerPrivKey"`
ProducerPrivKeySchema string `yaml:"producerPrivKeySchema"`
SignatureScheme []string `yaml:"signatureScheme"`
EmptyGenesis bool `yaml:"emptyGenesis"`
GravityChainDB db.Config `yaml:"gravityChainDB"`
Committee committee.Config `yaml:"committee"`

EnableTrielessStateDB bool `yaml:"enableTrielessStateDB"`
// EnableStateDBCaching enables cachedStateDBOption
Expand Down Expand Up @@ -77,22 +77,23 @@ type (
var (
// DefaultConfig is the default config of chain
DefaultConfig = Config{
ChainDBPath: "/var/data/chain.db",
TrieDBPatchFile: "/var/data/trie.db.patch",
TrieDBPath: "/var/data/trie.db",
StakingPatchDir: "/var/data",
IndexDBPath: "/var/data/index.db",
BloomfilterIndexDBPath: "/var/data/bloomfilter.index.db",
CandidateIndexDBPath: "/var/data/candidate.index.db",
StakingIndexDBPath: "/var/data/staking.index.db",
SGDIndexDBPath: "/var/data/sgd.index.db",
ID: 1,
EVMNetworkID: 4689,
Address: "",
ProducerPrivKey: generateRandomKey(SigP256k1),
SignatureScheme: []string{SigP256k1},
EmptyGenesis: false,
GravityChainDB: db.Config{DbPath: "/var/data/poll.db", NumRetries: 10},
ChainDBPath: "/var/data/chain.db",
TrieDBPatchFile: "/var/data/trie.db.patch",
TrieDBPath: "/var/data/trie.db",
StakingPatchDir: "/var/data",
IndexDBPath: "/var/data/index.db",
BloomfilterIndexDBPath: "/var/data/bloomfilter.index.db",
CandidateIndexDBPath: "/var/data/candidate.index.db",
StakingIndexDBPath: "/var/data/staking.index.db",
SGDIndexDBPath: "/var/data/sgd.index.db",
ContractStakingIndexDBPath: "/var/data/contractstaking.index.db",
ID: 1,
EVMNetworkID: 4689,
Address: "",
ProducerPrivKey: generateRandomKey(SigP256k1),
SignatureScheme: []string{SigP256k1},
EmptyGenesis: false,
GravityChainDB: db.Config{DbPath: "/var/data/poll.db", NumRetries: 10},
Committee: committee.Config{
GravityChainAPIs: []string{},
},
Expand Down
4 changes: 4 additions & 0 deletions blockchain/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ type (
SystemStakingContractAddress string `yaml:"systemStakingContractAddress"`
// SystemStakingContractHeight is the height of system staking contract
SystemStakingContractHeight uint64 `yaml:"systemStakingContractHeight"`
// SystemSGDContractAddress is the address of system sgd contract
SystemSGDContractAddress string `yaml:"sgdContractAddress"`
// SystemSGDContractHeight is the height of system sgd contract
SystemSGDContractHeight uint64 `yaml:"sgdContractHeight"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yaml name should be same as var, with first letter change to small

}
// Delegate defines a delegate with address and votes
Delegate struct {
Expand Down
19 changes: 13 additions & 6 deletions blockindex/sgd_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,17 @@ var _sgdCurrentHeight = []byte("currentHeight")
type (
// SGDRegistry is the interface for Sharing of Gas-fee with DApps
SGDRegistry interface {
blockdao.BlockIndexer
blockdao.BlockIndexerWithStart
// CheckContract returns the contract's eligibility for SGD and percentage
CheckContract(context.Context, string) (address.Address, uint64, bool, error)
// FetchContracts returns all contracts that are eligible for SGD
FetchContracts(context.Context) ([]*SGDIndex, error)
}

sgdRegistry struct {
contract string
kvStore db.KVStore
contract string
startHeight uint64
kvStore db.KVStore
}
// SGDIndex is the struct for SGDIndex
SGDIndex struct {
Expand Down Expand Up @@ -234,7 +235,7 @@ func newSgdIndex(contract, receiver []byte) *indexpb.SGDIndex {
}

// NewSGDRegistry creates a new SGDIndexer
func NewSGDRegistry(contract string, kv db.KVStore) SGDRegistry {
func NewSGDRegistry(contract string, startHeight uint64, kv db.KVStore) SGDRegistry {
if kv == nil {
panic("nil kvstore")
}
Expand All @@ -244,8 +245,9 @@ func NewSGDRegistry(contract string, kv db.KVStore) SGDRegistry {
}
}
return &sgdRegistry{
contract: contract,
kvStore: kv,
contract: contract,
startHeight: startHeight,
kvStore: kv,
}
}

Expand Down Expand Up @@ -276,6 +278,11 @@ func (sgd *sgdRegistry) Height() (uint64, error) {
return byteutil.BytesToUint64BigEndian(h), nil
}

// StartHeight returns the start height of the indexer
func (s *sgdRegistry) StartHeight() uint64 {
return s.startHeight
}

// PutBlock puts a block into SGDIndexer
func (sgd *sgdRegistry) PutBlock(ctx context.Context, blk *block.Block) error {
var (
Expand Down
2 changes: 1 addition & 1 deletion chainservice/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func (builder *Builder) buildSGDRegistry(forTest bool) error {
if err != nil {
return err
}
builder.cs.sgdIndexer = blockindex.NewSGDRegistry(builder.cfg.Chain.SGDContract, kvStore)
builder.cs.sgdIndexer = blockindex.NewSGDRegistry(builder.cfg.Genesis.SystemSGDContractAddress, builder.cfg.Genesis.SystemSGDContractHeight, kvStore)
}
return nil
}
Expand Down
4 changes: 4 additions & 0 deletions e2etest/local_actpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,23 @@ func newActPoolConfig(t *testing.T) (config.Config, error) {
r.NoError(err)
testIndexPath, err := testutil.PathOfTempFile("index")
r.NoError(err)
testContractIndexPath, err := testutil.PathOfTempFile("contractindex")
r.NoError(err)
testSGDIndexPath, err := testutil.PathOfTempFile("sgdindex")
r.NoError(err)
defer func() {
testutil.CleanupPath(testTriePath)
testutil.CleanupPath(testDBPath)
testutil.CleanupPath(testIndexPath)
testutil.CleanupPath(testContractIndexPath)
testutil.CleanupPath(testSGDIndexPath)
}()

cfg.Chain.TrieDBPatchFile = ""
cfg.Chain.TrieDBPath = testTriePath
cfg.Chain.ChainDBPath = testDBPath
cfg.Chain.IndexDBPath = testIndexPath
cfg.Chain.ContractStakingIndexDBPath = testContractIndexPath
cfg.Chain.SGDIndexDBPath = testSGDIndexPath
cfg.ActPool.MinGasPriceStr = "0"
cfg.Consensus.Scheme = config.NOOPScheme
Expand Down
18 changes: 18 additions & 0 deletions e2etest/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,21 @@ func TestLocalCommit(t *testing.T) {
require.NoError(err)
indexDBPath, err := testutil.PathOfTempFile(_dBPath)
require.NoError(err)
contractIndexDBPath, err := testutil.PathOfTempFile(_dBPath)
require.NoError(err)
indexSGDDBPath, err := testutil.PathOfTempFile(_dBPath + "_sgd")
require.NoError(err)
cfg.Chain.TrieDBPatchFile = ""
cfg.Chain.TrieDBPath = testTriePath
cfg.Chain.ChainDBPath = testDBPath
cfg.Chain.IndexDBPath = indexDBPath
cfg.Chain.ContractStakingIndexDBPath = contractIndexDBPath
cfg.Chain.SGDIndexDBPath = indexSGDDBPath
defer func() {
testutil.CleanupPath(testTriePath)
testutil.CleanupPath(testDBPath)
testutil.CleanupPath(indexDBPath)
testutil.CleanupPath(contractIndexDBPath)
testutil.CleanupPath(indexSGDDBPath)
}()

Expand Down Expand Up @@ -327,17 +331,21 @@ func TestLocalSync(t *testing.T) {
require.NoError(err)
indexDBPath, err := testutil.PathOfTempFile(_dBPath)
require.NoError(err)
contractIndexDBPath, err := testutil.PathOfTempFile(_dBPath)
require.NoError(err)
indexSGDDBPath, err := testutil.PathOfTempFile(_dBPath + "_sgd")
require.NoError(err)
cfg.Chain.TrieDBPatchFile = ""
cfg.Chain.TrieDBPath = testTriePath
cfg.Chain.ChainDBPath = testDBPath
cfg.Chain.IndexDBPath = indexDBPath
cfg.Chain.ContractStakingIndexDBPath = contractIndexDBPath
cfg.Chain.SGDIndexDBPath = indexSGDDBPath
defer func() {
testutil.CleanupPath(testTriePath)
testutil.CleanupPath(testDBPath)
testutil.CleanupPath(indexDBPath)
testutil.CleanupPath(contractIndexDBPath)
testutil.CleanupPath(indexSGDDBPath)
}()

Expand Down Expand Up @@ -387,6 +395,8 @@ func TestLocalSync(t *testing.T) {
require.NoError(err)
indexDBPath2, err := testutil.PathOfTempFile(_dBPath2)
require.NoError(err)
contractIndexDBPath2, err := testutil.PathOfTempFile(_dBPath2)
require.NoError(err)
indexSGDDBPath2, err := testutil.PathOfTempFile(_dBPath2 + "_sgd")
require.NoError(err)
cfg, err = newTestConfig()
Expand All @@ -395,11 +405,13 @@ func TestLocalSync(t *testing.T) {
cfg.Chain.TrieDBPath = testTriePath2
cfg.Chain.ChainDBPath = testDBPath2
cfg.Chain.IndexDBPath = indexDBPath2
cfg.Chain.ContractStakingIndexDBPath = contractIndexDBPath2
cfg.Chain.SGDIndexDBPath = indexSGDDBPath2
defer func() {
testutil.CleanupPath(testTriePath2)
testutil.CleanupPath(testDBPath2)
testutil.CleanupPath(indexDBPath2)
testutil.CleanupPath(contractIndexDBPath2)
testutil.CleanupPath(indexSGDDBPath2)
}()

Expand Down Expand Up @@ -449,6 +461,8 @@ func TestStartExistingBlockchain(t *testing.T) {
require.NoError(err)
testIndexPath, err := testutil.PathOfTempFile(_dBPath)
require.NoError(err)
testContractStakeIndexPath, err := testutil.PathOfTempFile(_dBPath)
require.NoError(err)
testSGDIndexPath, err := testutil.PathOfTempFile(_dBPath + "_sgd")
require.NoError(err)
// Disable block reward to make bookkeeping easier
Expand All @@ -457,6 +471,7 @@ func TestStartExistingBlockchain(t *testing.T) {
cfg.Chain.TrieDBPath = testTriePath
cfg.Chain.ChainDBPath = testDBPath
cfg.Chain.IndexDBPath = testIndexPath
cfg.Chain.ContractStakingIndexDBPath = testContractStakeIndexPath
cfg.Chain.SGDIndexDBPath = testSGDIndexPath
cfg.Chain.EnableAsyncIndexWrite = false
cfg.ActPool.MinGasPriceStr = "0"
Expand All @@ -478,6 +493,7 @@ func TestStartExistingBlockchain(t *testing.T) {
testutil.CleanupPath(testTriePath)
testutil.CleanupPath(testDBPath)
testutil.CleanupPath(testIndexPath)
testutil.CleanupPath(testContractStakeIndexPath)
testutil.CleanupPath(testSGDIndexPath)
}()

Expand Down Expand Up @@ -509,6 +525,7 @@ func TestStartExistingBlockchain(t *testing.T) {

// Build states from height 1 to 3
testutil.CleanupPath(testTriePath)
testutil.CleanupPath(testContractStakeIndexPath)
testutil.CleanupPath(testSGDIndexPath)
svr, err = itx.NewServer(cfg)
require.NoError(err)
Expand All @@ -530,6 +547,7 @@ func TestStartExistingBlockchain(t *testing.T) {
require.NoError(dao.DeleteBlockToTarget(2))
require.NoError(dao.Stop(ctx))
testutil.CleanupPath(testTriePath)
testutil.CleanupPath(testContractStakeIndexPath)
testutil.CleanupPath(testSGDIndexPath)
svr, err = itx.NewServer(cfg)
require.NoError(err)
Expand Down
5 changes: 4 additions & 1 deletion e2etest/local_transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ func TestLocalTransfer(t *testing.T) {
require.NoError(err)
testCandidateIndexPath, err := testutil.PathOfTempFile("candidateIndex")
require.NoError(err)
testContractStakeIndexPath, err := testutil.PathOfTempFile("contractStakeIndex")
require.NoError(err)
sgdIndexDBPath, err := testutil.PathOfTempFile("sgdIndex")
require.NoError(err)

Expand All @@ -311,12 +313,13 @@ func TestLocalTransfer(t *testing.T) {
testutil.CleanupPath(testSystemLogPath)
testutil.CleanupPath(testBloomfilterIndexPath)
testutil.CleanupPath(testCandidateIndexPath)
testutil.CleanupPath(testContractStakeIndexPath)
testutil.CleanupPath(sgdIndexDBPath)
}()

networkPort := 4689
apiPort := testutil.RandomPort()
cfg, err := newTransferConfig(testDBPath, testTriePath, testIndexPath, sgdIndexDBPath, testBloomfilterIndexPath, testSystemLogPath, testCandidateIndexPath, networkPort, apiPort)
cfg, err := newTransferConfig(testDBPath, testTriePath, testIndexPath, sgdIndexDBPath, testBloomfilterIndexPath, testSystemLogPath, testCandidateIndexPath, testContractStakeIndexPath, networkPort, apiPort)
defer func() {
delete(cfg.Plugins, config.GatewayPlugin)
}()
Expand Down
1 change: 1 addition & 0 deletions e2etest/native_staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ func TestNativeStaking(t *testing.T) {
cfg.Chain.TrieDBPath = testTriePath
cfg.Chain.ChainDBPath = testDBPath
cfg.Chain.IndexDBPath = testIndexPath
cfg.Chain.ContractStakingIndexDBPath = testIndexPath
cfg.Chain.SGDIndexDBPath = testSGDIndexPath
cfg.System.SystemLogDBPath = testSystemLogPath
cfg.Consensus.Scheme = config.NOOPScheme
Expand Down
5 changes: 5 additions & 0 deletions e2etest/nodeinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func newConfigForNodeInfoTest(triePath, dBPath, idxDBPath, contractIdxDBPath str
if err != nil {
return cfg, nil, err
}
contractIndexDBPath, err := testutil.PathOfTempFile(contractIdxDBPath)
if err != nil {
return cfg, nil, err
}
sgdIndexDBPath, err := testutil.PathOfTempFile(idxDBPath)
if err != nil {
return cfg, nil, err
Expand All @@ -44,6 +48,7 @@ func newConfigForNodeInfoTest(triePath, dBPath, idxDBPath, contractIdxDBPath str
cfg.Chain.TrieDBPath = testTriePath
cfg.Chain.ChainDBPath = testDBPath
cfg.Chain.IndexDBPath = indexDBPath
cfg.Chain.ContractStakingIndexDBPath = contractIndexDBPath
cfg.Chain.SGDIndexDBPath = sgdIndexDBPath
return cfg, func() {
testutil.CleanupPath(testTriePath)
Expand Down
3 changes: 3 additions & 0 deletions e2etest/rewarding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func TestBlockReward(t *testing.T) {
r.NoError(err)
testConsensusPath, err := testutil.PathOfTempFile("cons")
r.NoError(err)
testContractIndexPath, err := testutil.PathOfTempFile("contractindex")
r.NoError(err)
testSGDIndexPath, err := testutil.PathOfTempFile("sgdindex")
r.NoError(err)
cfg := config.Default
Expand All @@ -94,6 +96,7 @@ func TestBlockReward(t *testing.T) {
cfg.Chain.TrieDBPath = testTriePath
cfg.Chain.ChainDBPath = testDBPath
cfg.Chain.IndexDBPath = testIndexPath
cfg.Chain.ContractStakingIndexDBPath = testContractIndexPath
cfg.Chain.SGDIndexDBPath = testSGDIndexPath
cfg.Network.Port = testutil.RandomPort()
cfg.Genesis.PollMode = "lifeLong"
Expand Down
2 changes: 1 addition & 1 deletion e2etest/sgd_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestSGDRegistry(t *testing.T) {
r.NoError(err)
kvstore, err := db.CreateKVStore(db.DefaultConfig, indexSGDDBPath)
r.NoError(err)
sgdRegistry := blockindex.NewSGDRegistry(contractAddress, kvstore)
sgdRegistry := blockindex.NewSGDRegistry(contractAddress, 0, kvstore)
r.NoError(sgdRegistry.Start(ctx))
defer func() {
r.NoError(sgdRegistry.Stop(ctx))
Expand Down
1 change: 1 addition & 0 deletions e2etest/staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ func TestStakingContract(t *testing.T) {
testutil.CleanupPath(testCandidateIndexPath)
testutil.CleanupPath(testSystemLogPath)
testutil.CleanupPath(testConsensusPath)
testutil.CleanupPath(testContractStakeIndexPath)
testutil.CleanupPath(testSGDIndexPath)
// clear the gateway
delete(cfg.Plugins, config.GatewayPlugin)
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.