diff --git a/addresses/mainnet.json b/addresses/mainnet.json index 81462089..c3957c17 100644 --- a/addresses/mainnet.json +++ b/addresses/mainnet.json @@ -1,12 +1,12 @@ { - "Governance": "0xAc40E8dA38B5a381008059a38bb9bf45f0D3C4e2", - "BlockManager": "0x11aB70d78f1Dd2c3F967180d8A64858Db03A0aBa", - "CollectionManager": "0x367962d1462C568A0dDd0e2448311469451bF5a3", - "StakeManager": "0xe0bC695203d9C9f379bcdE9260B9F71B64B85298", - "RewardManager": "0x301779E4c95f951D2E17B4c3EdDC717551df2EBb", - "VoteManager": "0x641BAD0641eB5B94B19568C0a22a55AEbDAF1870", - "Delegator": "0xC74745eA5a3fac1864FAcd8f48d72C21A4ab883D", - "RAZOR": "0xcbf70914Fae03B3acB91E953De60CfDAaCA8145f", - "StakedTokenFactory": "0xEffA78888Dc1b6033286E5dF9b170bc5223178AB", - "RandomNoManager": "0xC6eF45F5Add040800D30FE6dEe01b4EBC4BfC467" + "Governance": "0xb3bFD9c67f0C58076Dc35d70C7A4Dae3640d0BFA", + "BlockManager": "0x8DEF3d7e68f32bB4d1247CA27847c1b2080247a8", + "CollectionManager": "0xCb537fA163873977035Aca160B9a1b23e6697f55", + "StakeManager": "0x66BFfDC1dfC6733503193eE0e2D493212362CD27", + "RewardManager": "0x6Dc96056A63F057B561C5841fE702a4582b320Da", + "VoteManager": "0x938157B936Edf416e52Ec0A1948E3ECd9C13B87B", + "Delegator": "0xB26FD23E7c20767fF5dCb883209b801f123C5060", + "RAZOR": "0x880e4aB635889b23c80179700D03c369b05fb500", + "StakedTokenFactory": "0x2fED450834aAEB618259619F975034fCBb04A6aF", + "RandomNoManager": "0xC3228f851fb49413F8100026c4085E02Bc6eBF06" } \ No newline at end of file diff --git a/addresses/testnet.json b/addresses/testnet.json index a7078572..73903b1d 100644 --- a/addresses/testnet.json +++ b/addresses/testnet.json @@ -1,13 +1,12 @@ { - "Governance": "0x0b9CC11E7f7D3D4f9bBc0cCaB85b73B96C322c78", - "BlockManager": "0x076df1c2d81C40D30DF115Ab68A13251fBD3FFA4", - "CollectionManager": "0x83f4D7ad6dD366c3F04Af45a1437c77636b03388", - "StakeManager": "0xf99a873a6afdF1b24388ac02ea0f1CFE3c70A80b", - "RewardManager": "0x522d2A51639332388dA4788DF59fB4E598278fAd", - "VoteManager": "0x2399D4d92b4D8762971605d0bC9597F780852CC4", - "Delegator": "0x81c72fB60d19Bfc3A0adDfE394501150d290cd66", - "RAZOR": "0x504C6635af1e7E6fdc586c8A89973783b9281A77", - "StakedTokenFactory": "0xf271bd91104946Cb8e43BC0e83423ed073ab136e", - "RandomNoManager": "0x2dc9E4663675c933f78D97cDD44463ee9C43144c", - "DelegatorV2": "0x4e9E5AE58Fb6d7Ca5b79A196b8d0FCF7CA15C100" + "Governance": "0x0C91e21D91510A1e95cd3507474c0ac1F620389d", + "BlockManager": "0x1eB1aD4C24cf2CA2027C05a28e78A117fDACD9F9", + "CollectionManager": "0xAFC593871Ab8EE89fd79B2ebd9F1f502268732b9", + "StakeManager": "0x7ce95141358FeF127d3F79f7a5C21C6c2afa1cA3", + "RewardManager": "0xbaa2e7509AdB5762f33064Ad762dF1136184Ef7D", + "VoteManager": "0x57a996015D831F6f4818ff962ffE405b6c6d4B21", + "Delegator": "0x63F82745aBe00c43a71ff2A48E65B281f72Bb54D", + "RAZOR": "0x91d07a181e2Fb787204Ae82B28c69e278C05ac70", + "StakedTokenFactory": "0x047f5865e666DCc278ad3bd8de8503D35d5278Ea", + "RandomNoManager": "0x3A6bbfBBe24F5F5D20F6a7265A0F602d155C2471" } \ No newline at end of file diff --git a/cache/cache.go b/cache/cache.go index ead6250e..fd1c766e 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -11,53 +11,17 @@ type cachedData struct { } type LocalCache struct { - stop chan struct{} - - wg sync.WaitGroup mu sync.RWMutex URLs map[string]cachedData //URLs } -func NewLocalCache(cleanupInterval time.Duration) *LocalCache { - lc := &LocalCache{ +// NewLocalCache creates a new LocalCache instance +func NewLocalCache() *LocalCache { + return &LocalCache{ URLs: make(map[string]cachedData), - stop: make(chan struct{}), - } - - lc.wg.Add(1) - go func(cleanupInterval time.Duration) { - defer lc.wg.Done() - lc.cleanupLoop(cleanupInterval) - }(cleanupInterval) - - return lc -} - -func (lc *LocalCache) cleanupLoop(interval time.Duration) { - t := time.NewTicker(interval) - defer t.Stop() - - for { - select { - case <-lc.stop: - return - case <-t.C: - lc.mu.Lock() - for url, cu := range lc.URLs { - if cu.expireAtTimestamp <= time.Now().Unix() { - delete(lc.URLs, url) - } - } - lc.mu.Unlock() - } } } -func (lc *LocalCache) StopCleanup() { - close(lc.stop) - lc.wg.Wait() -} - func (lc *LocalCache) Update(data []byte, url string, expireAtTimestamp int64) { lc.mu.Lock() defer lc.mu.Unlock() @@ -80,3 +44,26 @@ func (lc *LocalCache) Read(url string) ([]byte, bool) { return cacheData.Result, true } + +// ClearAll deletes all entries in the cache +func (lc *LocalCache) ClearAll() { + lc.mu.Lock() + defer lc.mu.Unlock() + + for key := range lc.URLs { + delete(lc.URLs, key) + } +} + +// Cleanup removes expired cache entries +func (lc *LocalCache) Cleanup() { + lc.mu.Lock() + defer lc.mu.Unlock() + + for url, data := range lc.URLs { + // Remove expired data after the expireAtTimestamp is passed + if data.expireAtTimestamp <= time.Now().Unix() { + delete(lc.URLs, url) + } + } +} diff --git a/cmd/cmd-utils.go b/cmd/cmd-utils.go index 2583c358..7786ac76 100644 --- a/cmd/cmd-utils.go +++ b/cmd/cmd-utils.go @@ -23,6 +23,10 @@ func (*UtilsStruct) GetEpochAndState(client *ethclient.Client) (uint32, int64, e if err != nil { return 0, 0, err } + err = ValidateBufferPercentLimit(client, bufferPercent) + if err != nil { + return 0, 0, err + } latestHeader, err := clientUtils.GetLatestBlockWithRetry(client) if err != nil { log.Error("Error in fetching block: ", err) diff --git a/cmd/cmd-utils_test.go b/cmd/cmd-utils_test.go index 0da68b1e..f91d1452 100644 --- a/cmd/cmd-utils_test.go +++ b/cmd/cmd-utils_test.go @@ -20,6 +20,8 @@ func TestGetEpochAndState(t *testing.T) { latestHeaderErr error bufferPercent int32 bufferPercentErr error + stateBuffer uint64 + stateBufferErr error state int64 stateErr error stateName string @@ -37,6 +39,7 @@ func TestGetEpochAndState(t *testing.T) { epoch: 4, latestHeader: &Types.Header{}, bufferPercent: 20, + stateBuffer: 5, state: 0, stateName: "commit", }, @@ -50,6 +53,7 @@ func TestGetEpochAndState(t *testing.T) { epochErr: errors.New("epoch error"), latestHeader: &Types.Header{}, bufferPercent: 20, + stateBuffer: 5, state: 0, stateName: "commit", }, @@ -76,6 +80,7 @@ func TestGetEpochAndState(t *testing.T) { epoch: 4, latestHeader: &Types.Header{}, bufferPercent: 20, + stateBuffer: 5, stateErr: errors.New("state error"), }, wantEpoch: 0, @@ -88,6 +93,7 @@ func TestGetEpochAndState(t *testing.T) { epoch: 4, latestHeaderErr: errors.New("header error"), bufferPercent: 20, + stateBuffer: 5, state: 0, stateName: "commit", }, @@ -95,6 +101,30 @@ func TestGetEpochAndState(t *testing.T) { wantState: 0, wantErr: errors.New("header error"), }, + { + name: "Test 6: When validating buffer percent limit fails", + args: args{ + epoch: 4, + latestHeader: &Types.Header{}, + bufferPercent: 50, + stateBuffer: 10, + }, + wantEpoch: 0, + wantState: 0, + wantErr: errors.New("buffer percent exceeds limit"), + }, + { + name: "Test 7: When there is an error in validating buffer percent limit", + args: args{ + epoch: 4, + latestHeader: &Types.Header{}, + bufferPercent: 50, + stateBufferErr: errors.New("state buffer error"), + }, + wantEpoch: 0, + wantState: 0, + wantErr: errors.New("state buffer error"), + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -102,6 +132,7 @@ func TestGetEpochAndState(t *testing.T) { utilsMock.On("GetEpoch", mock.AnythingOfType("*ethclient.Client")).Return(tt.args.epoch, tt.args.epochErr) cmdUtilsMock.On("GetBufferPercent").Return(tt.args.bufferPercent, tt.args.bufferPercentErr) + utilsMock.On("GetStateBuffer", mock.Anything).Return(tt.args.stateBuffer, tt.args.stateBufferErr) clientUtilsMock.On("GetLatestBlockWithRetry", mock.Anything).Return(tt.args.latestHeader, tt.args.latestHeaderErr) utilsMock.On("GetBufferedState", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.state, tt.args.stateErr) diff --git a/cmd/commit.go b/cmd/commit.go index df340bf5..0e2a6876 100644 --- a/cmd/commit.go +++ b/cmd/commit.go @@ -4,16 +4,13 @@ package cmd import ( "encoding/hex" "errors" + Types "github.com/ethereum/go-ethereum/core/types" "math/big" - "razor/cache" "razor/core" "razor/core/types" "razor/pkg/bindings" "razor/utils" "sync" - "time" - - Types "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/ethclient" @@ -79,8 +76,8 @@ func (*UtilsStruct) HandleCommitState(client *ethclient.Client, epoch uint32, se var wg sync.WaitGroup - log.Debug("Creating a local cache which will store API result and expire at the end of commit state") - commitParams.LocalCache = cache.NewLocalCache(time.Second * time.Duration(core.StateLength)) + // Clean up any expired API results cache data before performing the commit + commitParams.LocalCache.Cleanup() log.Debug("Iterating over all the collections...") for i := 0; i < int(numActiveCollections); i++ { @@ -129,7 +126,6 @@ func (*UtilsStruct) HandleCommitState(client *ethclient.Client, epoch uint32, se if err != nil { // Returning the first error from the error channel log.Error("Error in getting collection data: ", err) - commitParams.LocalCache.StopCleanup() return types.CommitData{}, err } } @@ -139,8 +135,6 @@ func (*UtilsStruct) HandleCommitState(client *ethclient.Client, epoch uint32, se log.Debug("HandleCommitState: SeqAllottedCollections: ", seqAllottedCollections) log.Debug("HandleCommitState: Leaves: ", leavesOfTree) - commitParams.LocalCache.StopCleanup() - return types.CommitData{ AssignedCollections: assignedCollections, SeqAllottedCollections: seqAllottedCollections, diff --git a/cmd/commit_test.go b/cmd/commit_test.go index bba1409a..a998ec22 100644 --- a/cmd/commit_test.go +++ b/cmd/commit_test.go @@ -16,7 +16,6 @@ import ( "razor/utils" "reflect" "testing" - "time" ) func TestCommit(t *testing.T) { @@ -226,7 +225,7 @@ func TestHandleCommitState(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - localCache := cache.NewLocalCache(time.Second * 10) + localCache := cache.NewLocalCache() commitParams := &types.CommitParams{ LocalCache: localCache, } @@ -396,7 +395,7 @@ func BenchmarkHandleCommitState(b *testing.B) { for _, v := range table { b.Run(fmt.Sprintf("Number_Of_Active_Collections%d", v.numActiveCollections), func(b *testing.B) { for i := 0; i < b.N; i++ { - localCache := cache.NewLocalCache(time.Second * 10) + localCache := cache.NewLocalCache() commitParams := &types.CommitParams{ LocalCache: localCache, } diff --git a/cmd/config-utils.go b/cmd/config-utils.go index a5e5839a..7518d222 100644 --- a/cmd/config-utils.go +++ b/cmd/config-utils.go @@ -3,6 +3,7 @@ package cmd import ( "errors" + "github.com/ethereum/go-ethereum/ethclient" "github.com/sirupsen/logrus" "razor/client" "razor/core" @@ -207,11 +208,6 @@ func (*UtilsStruct) GetMultiplier() (float32, error) { //This function returns the buffer percent func (*UtilsStruct) GetBufferPercent() (int32, error) { - const ( - MinBufferPercent = 0 - MaxBufferPercent = 30 - ) - bufferPercent, err := getConfigValue("buffer", "int32", core.DefaultBufferPercent, "buffer") if err != nil { return core.DefaultBufferPercent, err @@ -219,16 +215,10 @@ func (*UtilsStruct) GetBufferPercent() (int32, error) { bufferPercentInt32 := bufferPercent.(int32) - // Check if bufferPercent is explicitly set and not within the valid range. - if bufferPercentInt32 < MinBufferPercent || bufferPercentInt32 > MaxBufferPercent { - log.Infof("BufferPercent %d is out of the valid range (%d-%d), using default value %d", bufferPercentInt32, MinBufferPercent, MaxBufferPercent, core.DefaultBufferPercent) - return core.DefaultBufferPercent, nil - } - - // If bufferPercent is 0, use the default value. - if bufferPercentInt32 == 0 { - log.Debugf("BufferPercent is unset or set to 0, using its default %d value", core.DefaultBufferPercent) - return core.DefaultBufferPercent, nil + // bufferPercent cannot be less than core.DefaultBufferPercent else through an error + if bufferPercentInt32 < core.DefaultBufferPercent { + log.Infof("BufferPercent should be greater than or equal to %v", core.DefaultBufferPercent) + return core.DefaultBufferPercent, errors.New("invalid buffer percent") } return bufferPercentInt32, nil @@ -334,7 +324,7 @@ func (*UtilsStruct) GetGasLimitOverride() (uint64, error) { func (*UtilsStruct) GetRPCTimeout() (int64, error) { const ( MinRPCTimeout = 10 // Minimum RPC timeout in seconds - MaxRPCTimeout = 60 // Maximum RPC timeout in seconds + MaxRPCTimeout = 20 // Maximum RPC timeout in seconds ) rpcTimeout, err := getConfigValue("rpcTimeout", "int64", core.DefaultRPCTimeout, "rpcTimeout") @@ -356,7 +346,7 @@ func (*UtilsStruct) GetRPCTimeout() (int64, error) { func (*UtilsStruct) GetHTTPTimeout() (int64, error) { const ( MinHTTPTimeout = 10 // Minimum HTTP timeout in seconds - MaxHTTPTimeout = 60 // Maximum HTTP timeout in seconds + MaxHTTPTimeout = 20 // Maximum HTTP timeout in seconds ) httpTimeout, err := getConfigValue("httpTimeout", "int64", core.DefaultHTTPTimeout, "httpTimeout") @@ -413,3 +403,30 @@ func setLogLevel(config types.Configurations) { log.Debugf("Log File Max Age (max number of days to retain old log files): %d", config.LogFileMaxAge) } } + +func ValidateBufferPercentLimit(client *ethclient.Client, bufferPercent int32) error { + stateBuffer, err := razorUtils.GetStateBuffer(client) + if err != nil { + return err + } + maxBufferPercent := calculateMaxBufferPercent(stateBuffer, core.StateLength) + if bufferPercent >= maxBufferPercent { + log.Errorf("Buffer percent %v is greater than or equal to maximum possible buffer percent", maxBufferPercent) + return errors.New("buffer percent exceeds limit") + } + return nil +} + +// calculateMaxBuffer calculates the maximum buffer percent value. +func calculateMaxBufferPercent(stateBuffer, stateLength uint64) int32 { + if stateLength == 0 { + return 0 + } + + // The formula is derived from the condition: + // 2(maxBuffer % stateLength) < (stateLength - 2*StateBuffer) + + // Perform the calculation with float64 for precision + maxBufferPercent := 50 * (1 - (float64(2*stateBuffer) / float64(stateLength))) + return int32(maxBufferPercent) +} diff --git a/cmd/config-utils_test.go b/cmd/config-utils_test.go index da9e80f4..f7b5bbde 100644 --- a/cmd/config-utils_test.go +++ b/cmd/config-utils_test.go @@ -2,6 +2,7 @@ package cmd import ( "errors" + "github.com/ethereum/go-ethereum/ethclient" "github.com/spf13/viper" "github.com/stretchr/testify/mock" "os" @@ -260,9 +261,9 @@ func TestGetBufferPercent(t *testing.T) { name: "Test 1: When buffer percent is fetched from root flag", args: args{ isFlagSet: true, - bufferPercent: 5, + bufferPercent: 10, }, - want: 5, + want: 10, wantErr: nil, }, { @@ -278,9 +279,9 @@ func TestGetBufferPercent(t *testing.T) { name: "Test 3: When buffer value is fetched from config", useDummyConfigFile: true, args: args{ - bufferInTestConfig: 1, + bufferInTestConfig: 6, }, - want: 1, + want: 6, wantErr: nil, }, { @@ -292,10 +293,10 @@ func TestGetBufferPercent(t *testing.T) { name: "Test 5: When buffer value is out of a valid range", useDummyConfigFile: true, args: args{ - bufferInTestConfig: 40, + bufferInTestConfig: 0, }, want: core.DefaultBufferPercent, - wantErr: nil, + wantErr: errors.New("invalid buffer percent"), }, } for _, tt := range tests { @@ -1199,3 +1200,62 @@ func TestGetWaitTime(t *testing.T) { }) } } + +func TestValidateBufferPercentLimit(t *testing.T) { + var client *ethclient.Client + + type args struct { + bufferPercent int32 + stateBuffer uint64 + stateBufferErr error + } + tests := []struct { + name string + args args + wantErr error + }{ + { + name: "Buffer percent less than max buffer percent", + args: args{ + stateBuffer: 10, + bufferPercent: 20, + }, + wantErr: nil, + }, + { + name: "Buffer percent greater than max buffer percent", + args: args{ + stateBuffer: 10, + bufferPercent: 60, + }, + wantErr: errors.New("buffer percent exceeds limit"), + }, + { + name: "GetStateBuffer returns an error", + args: args{ + stateBufferErr: errors.New("state buffer error"), + bufferPercent: 10, + }, + wantErr: errors.New("state buffer error"), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + SetUpMockInterfaces() + + utilsMock.On("GetStateBuffer", mock.Anything).Return(tt.args.stateBuffer, tt.args.stateBufferErr) + + err := ValidateBufferPercentLimit(client, tt.args.bufferPercent) + if err == nil || tt.wantErr == nil { + if err != tt.wantErr { + t.Errorf("Error for GetEpochAndState function, got = %v, want = %v", err, tt.wantErr) + } + } else { + if err.Error() != tt.wantErr.Error() { + t.Errorf("Error for GetEpochAndState function, got = %v, want = %v", err, tt.wantErr) + } + } + + }) + } +} diff --git a/cmd/propose.go b/cmd/propose.go index 70030c81..43aa8265 100644 --- a/cmd/propose.go +++ b/cmd/propose.go @@ -76,11 +76,6 @@ func (*UtilsStruct) Propose(client *ethclient.Client, config types.Configuration } log.Debugf("Biggest staker Id: %d Biggest stake: %s, Stake: %s, Staker Id: %d, Number of Stakers: %d, Salt: %s", biggestStakerId, biggestStake, staker.Stake, staker.Id, numStakers, hex.EncodeToString(salt[:])) - - bufferPercent, err := cmdUtils.GetBufferPercent() - if err != nil { - return err - } proposer := types.ElectedProposer{ Stake: staker.Stake, StakerId: staker.Id, @@ -89,8 +84,8 @@ func (*UtilsStruct) Propose(client *ethclient.Client, config types.Configuration Salt: salt, Epoch: epoch, } - log.Debugf("Propose: Calling GetIteration with arguments proposer = %+v, buffer percent = %d", proposer, bufferPercent) - iteration := cmdUtils.GetIteration(client, proposer, bufferPercent) + log.Debugf("Propose: Calling GetIteration with arguments proposer = %+v, buffer percent = %d", proposer, config.BufferPercent) + iteration := cmdUtils.GetIteration(client, proposer, config.BufferPercent) log.Debug("Iteration: ", iteration) diff --git a/cmd/propose_test.go b/cmd/propose_test.go index c1837908..6ab69727 100644 --- a/cmd/propose_test.go +++ b/cmd/propose_test.go @@ -52,8 +52,6 @@ func TestPropose(t *testing.T) { smallestStakerIdErr error randaoHash [32]byte randaoHashErr error - bufferPercent int32 - bufferPercentErr error salt [32]byte saltErr error iteration int @@ -429,19 +427,7 @@ func TestPropose(t *testing.T) { wantErr: errors.New("error in saving data"), }, { - name: "Test 17: When there is an error in getting buffer percent", - args: args{ - state: 2, - staker: bindings.StructsStaker{}, - numStakers: 5, - biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)), - biggestStakerId: 2, - bufferPercentErr: errors.New("buffer error"), - }, - wantErr: errors.New("buffer error"), - }, - { - name: "Test 18: When rogue mode is on for biggestStakerId and propose exceutes successfully", + name: "Test 17: When rogue mode is on for biggestStakerId and propose exceutes successfully", args: args{ rogueData: types.Rogue{ IsRogue: true, @@ -468,7 +454,7 @@ func TestPropose(t *testing.T) { wantErr: nil, }, { - name: "Test 19: When rogue mode is on for biggestStakerId and there is an error in getting smallestStakerId", + name: "Test 18: When rogue mode is on for biggestStakerId and there is an error in getting smallestStakerId", args: args{ rogueData: types.Rogue{ IsRogue: true, @@ -494,7 +480,7 @@ func TestPropose(t *testing.T) { wantErr: errors.New("smallestStakerId error"), }, { - name: "Test 20: When there is an error in waitForCompletion", + name: "Test 19: When there is an error in waitForCompletion", args: args{ state: 2, staker: bindings.StructsStaker{}, @@ -539,7 +525,6 @@ func TestPropose(t *testing.T) { utilsMock.On("GetTxnOpts", mock.AnythingOfType("types.TransactionOptions")).Return(TxnOpts) blockManagerMock.On("Propose", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.proposeTxn, tt.args.proposeErr) transactionMock.On("Hash", mock.Anything).Return(tt.args.hash) - cmdUtilsMock.On("GetBufferPercent").Return(tt.args.bufferPercent, tt.args.bufferPercentErr) utilsMock.On("WaitForBlockCompletion", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("string")).Return(tt.args.waitForBlockCompletionErr) utils := &UtilsStruct{} diff --git a/cmd/vote.go b/cmd/vote.go index 4aa85cda..0dd69073 100644 --- a/cmd/vote.go +++ b/cmd/vote.go @@ -11,6 +11,7 @@ import ( "os/signal" "path/filepath" "razor/accounts" + "razor/cache" "razor/core" "razor/core/types" "razor/logger" @@ -53,6 +54,9 @@ func (*UtilsStruct) ExecuteVote(flagSet *pflag.FlagSet) { client := razorUtils.ConnectToClient(config.Provider) + err = ValidateBufferPercentLimit(client, config.BufferPercent) + utils.CheckError("Error in validating buffer percent: ", err) + address, err := flagSetUtils.GetStringAddress(flagSet) utils.CheckError("Error in getting address: ", err) log.Debug("ExecuteVote: Address: ", address) @@ -115,6 +119,7 @@ func (*UtilsStruct) ExecuteVote(flagSet *pflag.FlagSet) { utils.CheckError("Error in initializing asset cache: ", err) commitParams := &types.CommitParams{ + LocalCache: cache.NewLocalCache(), // Creating a local cache which will store API results JobsCache: jobsCache, CollectionsCache: collectionsCache, HttpClient: httpClient, @@ -418,6 +423,9 @@ func (*UtilsStruct) InitiateCommit(client *ethclient.Client, config types.Config log.Debug("Updating GlobalCommitDataStruct with latest commitData and epoch...") updateGlobalCommitDataStruct(commitData, epoch) log.Debugf("InitiateCommit: Global commit data struct: %+v", globalCommitDataStruct) + + log.Debug("Clearing up the cache storing API results after successful commit...") + commitParams.LocalCache.ClearAll() } return nil } diff --git a/cmd/vote_test.go b/cmd/vote_test.go index 26e8d218..4809a8ad 100644 --- a/cmd/vote_test.go +++ b/cmd/vote_test.go @@ -161,6 +161,7 @@ func TestExecuteVote(t *testing.T) { fileUtilsMock.On("AssignLogFile", mock.AnythingOfType("*pflag.FlagSet"), mock.Anything) cmdUtilsMock.On("GetConfigData").Return(tt.args.config, tt.args.configErr) + utilsMock.On("GetStateBuffer", mock.Anything).Return(uint64(5), nil) utilsMock.On("AssignPassword", flagSet).Return(tt.args.password) utilsMock.On("CheckPassword", mock.Anything).Return(nil) utilsMock.On("AccountManagerForKeystore").Return(&accounts.AccountManager{}, nil) @@ -344,6 +345,7 @@ func TestInitiateCommit(t *testing.T) { ) commitParams := &types.CommitParams{ + LocalCache: cache.NewLocalCache(), JobsCache: cache.NewJobsCache(), CollectionsCache: cache.NewCollectionsCache(), FromBlockToCheckForEvents: big.NewInt(1), diff --git a/config.sh b/config.sh index fc353a4c..de4ebd59 100644 --- a/config.sh +++ b/config.sh @@ -19,15 +19,15 @@ then GAS_MULTIPLIER=1.0 fi -read -rp "Buffer Percent: (20) " BUFFER +read -rp "Buffer Percent: (5) " BUFFER if [ -z "$BUFFER" ]; then - BUFFER=20 + BUFFER=5 fi -read -rp "Wait Time: (5) " WAIT_TIME +read -rp "Wait Time: (1) " WAIT_TIME if [ -z "$WAIT_TIME" ]; then - WAIT_TIME=5 + WAIT_TIME=1 fi read -rp "Gas Price: (1) " GAS_PRICE diff --git a/core/constants.go b/core/constants.go index d895a722..be91d650 100644 --- a/core/constants.go +++ b/core/constants.go @@ -9,13 +9,13 @@ import ( ) const ( - EpochLength uint64 = 1200 + EpochLength uint64 = 300 NumberOfStates uint64 = 5 StateLength = EpochLength / NumberOfStates ) -// ChainId corresponds to the SKALE chain -var ChainId = big.NewInt(0x109B4597) +// ChainId corresponds to the EUROPA chain +var ChainId = big.NewInt(0x79f99296) const MaxRetries uint = 8 @@ -26,9 +26,9 @@ const BlockCompletionTimeout = 30 //Following are the default config values for all the config parameters const ( DefaultGasMultiplier float32 = 1.0 - DefaultBufferPercent int32 = 20 + DefaultBufferPercent int32 = 5 DefaultGasPrice int32 = 1 - DefaultWaitTime int32 = 5 + DefaultWaitTime int32 = 1 DefaultGasLimit float32 = 2 DefaultGasLimitOverride uint64 = 30000000 DefaultRPCTimeout int64 = 10 @@ -37,7 +37,7 @@ const ( ) //BufferStateSleepTime is the sleeping time whenever buffer state hits -const BufferStateSleepTime int32 = 5 +const BufferStateSleepTime int32 = 2 //Following are the default logFile parameters in config const ( diff --git a/core/contracts.go b/core/contracts.go index 6079b6e4..0e3dc5b9 100644 --- a/core/contracts.go +++ b/core/contracts.go @@ -1,7 +1,7 @@ package core -var StakeManagerAddress = "0xe0bC695203d9C9f379bcdE9260B9F71B64B85298" -var RAZORAddress = "0xcbf70914Fae03B3acB91E953De60CfDAaCA8145f" -var CollectionManagerAddress = "0x367962d1462C568A0dDd0e2448311469451bF5a3" -var VoteManagerAddress = "0x641BAD0641eB5B94B19568C0a22a55AEbDAF1870" -var BlockManagerAddress = "0x11aB70d78f1Dd2c3F967180d8A64858Db03A0aBa" +var StakeManagerAddress = "0x66BFfDC1dfC6733503193eE0e2D493212362CD27" +var RAZORAddress = "0x880e4aB635889b23c80179700D03c369b05fb500" +var CollectionManagerAddress = "0xCb537fA163873977035Aca160B9a1b23e6697f55" +var VoteManagerAddress = "0x938157B936Edf416e52Ec0A1948E3ECd9C13B87B" +var BlockManagerAddress = "0x8DEF3d7e68f32bB4d1247CA27847c1b2080247a8" diff --git a/core/version.go b/core/version.go index b31b42ff..15a0e147 100644 --- a/core/version.go +++ b/core/version.go @@ -3,8 +3,8 @@ package core import "fmt" const ( - VersionMajor = 1 // Major version component of the current release - VersionMinor = 2 // Minor version component of the current release + VersionMajor = 2 // Major version component of the current release + VersionMinor = 0 // Minor version component of the current release VersionPatch = 0 // Patch version component of the current release VersionMeta = "" // Version metadata to append to the version string ) diff --git a/update-chainId.sh b/update-chainId.sh index 591a317d..2b4817e7 100644 --- a/update-chainId.sh +++ b/update-chainId.sh @@ -5,7 +5,7 @@ NETWORK=$1 CHAINID="" if [[ "$NETWORK" == "mainnet" ]]; then - CHAINID="0x109B4597" + CHAINID="0x79f99296" elif [[ "$NETWORK" == "testnet" ]]; then CHAINID="0x561bf78b" else diff --git a/utils/api.go b/utils/api.go index a64eb1a4..0f4f1d92 100644 --- a/utils/api.go +++ b/utils/api.go @@ -36,7 +36,7 @@ func GetDataFromAPI(commitParams *types.CommitParams, dataSourceURLStruct types. return nil, err } - // Storing the data into cache + // Storing the API results data into cache commitParams.LocalCache.Update(response, cacheKey, time.Now().Add(time.Second*time.Duration(core.StateLength)).Unix()) return response, nil } diff --git a/utils/api_test.go b/utils/api_test.go index 0d535523..70253a18 100644 --- a/utils/api_test.go +++ b/utils/api_test.go @@ -178,7 +178,7 @@ func TestGetDataFromAPI(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - localCache := cache.NewLocalCache(time.Second * 10) + localCache := cache.NewLocalCache() commitParams := &types.CommitParams{ LocalCache: localCache, HttpClient: httpClient, diff --git a/utils/asset_test.go b/utils/asset_test.go index e45bf831..cf4dfa74 100644 --- a/utils/asset_test.go +++ b/utils/asset_test.go @@ -644,7 +644,7 @@ func TestGetDataToCommitFromJobs(t *testing.T) { t.Run(tt.name, func(t *testing.T) { UtilsInterface = &UtilsStruct{} commitParams := &types.CommitParams{ - LocalCache: cache.NewLocalCache(time.Second * 10), + LocalCache: cache.NewLocalCache(), HttpClient: httpClient, } @@ -787,7 +787,7 @@ func TestGetDataToCommitFromJob(t *testing.T) { utils := StartRazor(optionsPackageStruct) commitParams := &types.CommitParams{ - LocalCache: cache.NewLocalCache(time.Second * 10), + LocalCache: cache.NewLocalCache(), HttpClient: httpClient, } diff --git a/utils/common_test.go b/utils/common_test.go index 4ab53a83..cea9ca0f 100644 --- a/utils/common_test.go +++ b/utils/common_test.go @@ -386,7 +386,7 @@ func TestGetBufferedState(t *testing.T) { stateBuffer: 5, }, - want: 0, + want: 1, wantErr: false, }, { @@ -398,7 +398,7 @@ func TestGetBufferedState(t *testing.T) { buffer: 2, stateBuffer: 5, }, - want: 4, + want: -1, wantErr: false, }, { @@ -411,7 +411,7 @@ func TestGetBufferedState(t *testing.T) { stateBuffer: 5, }, - want: 3, + want: -1, wantErr: false, }, { @@ -959,7 +959,7 @@ func TestGetRemainingTimeOfCurrentState(t *testing.T) { block: &types.Header{}, stateBuffer: 5, }, - want: 235, + want: 55, wantErr: false, }, { @@ -1086,7 +1086,7 @@ func TestEstimateBlockNumberAtEpochBeginning(t *testing.T) { block: &types.Header{Time: 1, Number: big.NewInt(1)}, previousBlock: &types.Header{Time: 20, Number: big.NewInt(1)}, }, - want: big.NewInt(-239), + want: big.NewInt(-59), wantErr: false, }, { diff --git a/utils/options.go b/utils/options.go index 21062e0c..a9239889 100644 --- a/utils/options.go +++ b/utils/options.go @@ -108,14 +108,24 @@ func (*GasStruct) GetGasLimit(transactionData types.TransactionOptions, txnOpts Value: txnOpts.Value, Data: inputData, } - gasLimit, err := ClientInterface.EstimateGasWithRetry(transactionData.Client, msg) - if err != nil { - log.Error("GetGasLimit: Error in getting gasLimit: ", err) - //If estimateGas throws an error for a transaction than gasLimit should be picked up from the config - log.Debugf("As there was an error from estimateGas, taking the gas limit value = %d from config", transactionData.Config.GasLimitOverride) - return transactionData.Config.GasLimitOverride, nil + var gasLimit uint64 + if transactionData.MethodName == "reveal" { + gasLimit, err = getGasLimitForReveal(transactionData.Client) + if err != nil { + log.Error("GetGasLimit: Error in getting gasLimit for reveal transaction: ", err) + return transactionData.Config.GasLimitOverride, err + } + log.Debug("Calculated gas limit for reveal: ", gasLimit) + } else { + gasLimit, err = ClientInterface.EstimateGasWithRetry(transactionData.Client, msg) + if err != nil { + log.Error("GetGasLimit: Error in getting gasLimit: ", err) + //If estimateGas throws an error for a transaction than gasLimit should be picked up from the config + log.Debugf("As there was an error from estimateGas, taking the gas limit value = %d from config", transactionData.Config.GasLimitOverride) + return transactionData.Config.GasLimitOverride, nil + } + log.Debug("Estimated Gas: ", gasLimit) } - log.Debug("Estimated Gas: ", gasLimit) return GasInterface.IncreaseGasLimitValue(transactionData.Client, gasLimit, transactionData.Config.GasLimitMultiplier) } @@ -138,3 +148,14 @@ func (*GasStruct) IncreaseGasLimitValue(client *ethclient.Client, gasLimit uint6 return gasLimit, nil } + +func getGasLimitForReveal(client *ethclient.Client) (uint64, error) { + toAssign, err := UtilsInterface.ToAssign(client) + if err != nil { + return 0, err + } + + // Apply the formula: gasLimit = 226864 + n * 85000 + gasLimit := 226864 + (uint64(toAssign) * 85000) + return gasLimit, nil +} diff --git a/utils/options_test.go b/utils/options_test.go index 54d7f907..71628fbd 100644 --- a/utils/options_test.go +++ b/utils/options_test.go @@ -345,6 +345,8 @@ func TestUtilsStruct_GetGasLimit(t *testing.T) { parseErr error inputData []byte packErr error + toAssign uint16 + toAssignErr error gasLimit uint64 gasLimitErr error increaseGasLimit uint64 @@ -423,6 +425,41 @@ func TestUtilsStruct_GetGasLimit(t *testing.T) { want: 5000000, wantErr: nil, }, + { + name: "Test 6: When the transaction is reveal and we get the calculated gasLimit for reveal txn", + args: args{ + transactionData: types.TransactionOptions{ + MethodName: "reveal", + Config: types.Configurations{ + GasLimitMultiplier: 2, + GasLimitOverride: 5000000, + }, + }, + parsedData: parsedData, + inputData: inputData, + toAssign: 3, + increaseGasLimit: 963728, + }, + want: 963728, + wantErr: nil, + }, + { + name: "Test 6: When the transaction is reveal and we get the error in getting toAssign during calculating gasLimit for reveal txn", + args: args{ + transactionData: types.TransactionOptions{ + MethodName: "reveal", + Config: types.Configurations{ + GasLimitMultiplier: 2, + GasLimitOverride: 5000000, + }, + }, + parsedData: parsedData, + inputData: inputData, + toAssignErr: errors.New("toAssign error"), + }, + want: 5000000, + wantErr: errors.New("toAssign error"), + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -445,6 +482,7 @@ func TestUtilsStruct_GetGasLimit(t *testing.T) { abiMock.On("Pack", parsedData, mock.AnythingOfType("string"), mock.Anything).Return(tt.args.inputData, tt.args.packErr) clientUtilsMock.On("EstimateGasWithRetry", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("ethereum.CallMsg")).Return(tt.args.gasLimit, tt.args.gasLimitErr) gasUtilsMock.On("IncreaseGasLimitValue", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("uint64"), mock.AnythingOfType("float32")).Return(tt.args.increaseGasLimit, tt.args.increaseGasLimitErr) + utilsMock.On("ToAssign", mock.Anything).Return(tt.args.toAssign, tt.args.toAssignErr) gasUtils := GasStruct{} got, err := gasUtils.GetGasLimit(tt.args.transactionData, txnOpts)