Skip to content

Commit

Permalink
refactor: removed fatal logs from GetTxnOpts() (#1261)
Browse files Browse the repository at this point in the history
* refactor: removed fatal logs from GetTxnOpts()

* refactor: removed fatal log when address not found

* reafctor: fixed tests
  • Loading branch information
Yashk767 authored Dec 13, 2024
1 parent 52d8d2a commit fd91fc4
Show file tree
Hide file tree
Showing 48 changed files with 713 additions and 370 deletions.
5 changes: 4 additions & 1 deletion cmd/addStake.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ func (*UtilsStruct) StakeCoins(rpcParameters rpc.RPCParameters, txnArgs types.Tr
txnArgs.MethodName = "stake"
txnArgs.Parameters = []interface{}{epoch, txnArgs.Amount}
txnArgs.ABI = bindings.StakeManagerMetaData.ABI
txnOpts := razorUtils.GetTxnOpts(rpcParameters, txnArgs)
txnOpts, err := razorUtils.GetTxnOpts(rpcParameters, txnArgs)
if err != nil {
return core.NilHash, err
}

client, err := rpcParameters.RPCManager.GetBestRPCClient()
if err != nil {
Expand Down
16 changes: 15 additions & 1 deletion cmd/addStake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestStakeCoins(t *testing.T) {
txnArgs types.TransactionOptions
epoch uint32
getEpochErr error
txnOptsErr error
stakeTxn *Types.Transaction
stakeErr error
hash common.Hash
Expand Down Expand Up @@ -80,12 +81,25 @@ func TestStakeCoins(t *testing.T) {
want: core.NilHash,
wantErr: errors.New("stake error"),
},
{
name: "Test 4: When there is an error in getting transaction options",
args: args{
txnArgs: types.TransactionOptions{
Amount: big.NewInt(1000),
},
epoch: 2,
getEpochErr: nil,
txnOptsErr: errors.New("txnOpts error"),
},
want: core.NilHash,
wantErr: errors.New("txnOpts error"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
SetUpMockInterfaces()
utilsMock.On("GetEpoch", mock.Anything).Return(tt.args.epoch, tt.args.getEpochErr)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts, tt.args.txnOptsErr)
transactionMock.On("Hash", mock.Anything).Return(tt.args.hash)
stakeManagerMock.On("Stake", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.stakeTxn, tt.args.stakeErr)

Expand Down
5 changes: 4 additions & 1 deletion cmd/approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ func (*UtilsStruct) Approve(rpcParameters rpc.RPCParameters, txnArgs types.Trans
txnArgs.MethodName = "approve"
txnArgs.ABI = bindings.RAZORMetaData.ABI
txnArgs.Parameters = []interface{}{common.HexToAddress(core.StakeManagerAddress), txnArgs.Amount}
txnOpts := razorUtils.GetTxnOpts(rpcParameters, txnArgs)
txnOpts, err := razorUtils.GetTxnOpts(rpcParameters, txnArgs)
if err != nil {
return core.NilHash, err
}

client, err := rpcParameters.RPCManager.GetBestRPCClient()
if err != nil {
Expand Down
16 changes: 15 additions & 1 deletion cmd/approve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestApprove(t *testing.T) {
callOpts bind.CallOpts
allowanceAmount *big.Int
allowanceError error
txnOptsErr error
approveTxn *Types.Transaction
approveError error
hash common.Hash
Expand Down Expand Up @@ -115,13 +116,26 @@ func TestApprove(t *testing.T) {
want: common.Hash{0},
wantErr: errors.New("approve error"),
},
{
name: "Test 5: When there is error in getting txnOpts",
args: args{
txnArgs: types.TransactionOptions{
Amount: big.NewInt(10000),
},
callOpts: bind.CallOpts{},
allowanceAmount: big.NewInt(0),
txnOptsErr: errors.New("txnOpts error"),
},
want: core.NilHash,
wantErr: errors.New("txnOpts error"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
SetUpMockInterfaces()

utilsMock.On("GetOptions").Return(tt.args.callOpts)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts, tt.args.txnOptsErr)
transactionMock.On("Hash", mock.Anything).Return(tt.args.hash)
utilsMock.On("Allowance", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.allowanceAmount, tt.args.allowanceError)
tokenManagerMock.On("Approve", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.approveTxn, tt.args.approveError)
Expand Down
5 changes: 4 additions & 1 deletion cmd/claimBounty.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ func (*UtilsStruct) ClaimBounty(rpcParameters rpc.RPCParameters, config types.Co
return core.NilHash, nil
}

txnOpts := razorUtils.GetTxnOpts(rpcParameters, txnArgs)
txnOpts, err := razorUtils.GetTxnOpts(rpcParameters, txnArgs)
if err != nil {
return core.NilHash, err
}
client, err := rpcParameters.RPCManager.GetBestRPCClient()
if err != nil {
return core.NilHash, err
Expand Down
16 changes: 15 additions & 1 deletion cmd/claimBounty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func TestClaimBounty(t *testing.T) {
epochErr error
bountyLock types.BountyLock
bountyLockErr error
txnOptsErr error
redeemBountyTxn *Types.Transaction
redeemBountyErr error
hash common.Hash
Expand Down Expand Up @@ -250,6 +251,19 @@ func TestClaimBounty(t *testing.T) {
want: core.NilHash,
wantErr: nil,
},
{
name: "Test 8: When there is an error in getting txnOpts",
args: args{
epoch: 70,
bountyLock: types.BountyLock{
Amount: big.NewInt(1000),
RedeemAfter: 70,
},
txnOptsErr: errors.New("txnOpts error"),
},
want: core.NilHash,
wantErr: errors.New("txnOpts error"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -267,7 +281,7 @@ func TestClaimBounty(t *testing.T) {
utilsMock.On("GetBountyLock", mock.Anything, mock.Anything).Return(tt.args.bountyLock, tt.args.bountyLockErr)
timeMock.On("Sleep", mock.AnythingOfType("time.Duration")).Return()
utilsMock.On("CalculateBlockTime", mock.AnythingOfType("*ethclient.Client")).Return(blockTime)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts, tt.args.txnOptsErr)
stakeManagerMock.On("RedeemBounty", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("*bind.TransactOpts"), mock.AnythingOfType("uint32")).Return(tt.args.redeemBountyTxn, tt.args.redeemBountyErr)
utilsMock.On("SecondsToReadableTime", mock.AnythingOfType("int")).Return(tt.args.time)
transactionUtilsMock.On("Hash", mock.Anything).Return(tt.args.hash)
Expand Down
3 changes: 2 additions & 1 deletion cmd/claimCommission.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (*UtilsStruct) ClaimCommission(flagSet *pflag.FlagSet) {
log.Debugf("ClaimCommission: Staker Info: %+v", stakerInfo)

if stakerInfo.StakerReward.Cmp(big.NewInt(0)) > 0 {
txnOpts := razorUtils.GetTxnOpts(rpcParameters, types.TransactionOptions{
txnOpts, err := razorUtils.GetTxnOpts(rpcParameters, types.TransactionOptions{
ChainId: core.ChainId,
Config: config,
ContractAddress: core.StakeManagerAddress,
Expand All @@ -47,6 +47,7 @@ func (*UtilsStruct) ClaimCommission(flagSet *pflag.FlagSet) {
ABI: bindings.StakeManagerMetaData.ABI,
Account: account,
})
utils.CheckError("Error in getting txn options: ", err)

log.Info("Claiming commission...")

Expand Down
2 changes: 1 addition & 1 deletion cmd/claimCommission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func TestClaimCommission(t *testing.T) {
utilsMock.On("CheckPassword", mock.Anything).Return(nil)
utilsMock.On("AccountManagerForKeystore").Return(&accounts.AccountManager{}, nil)
utilsMock.On("ConnectToClient", mock.AnythingOfType("string")).Return(client)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts, nil)
utilsMock.On("WaitForBlockCompletion", mock.Anything, mock.Anything).Return(nil)

utilsMock.On("StakerInfo", mock.Anything, mock.AnythingOfType("uint32")).Return(tt.args.stakerInfo, tt.args.stakerInfoErr)
Expand Down
5 changes: 4 additions & 1 deletion cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (*UtilsStruct) Commit(rpcParameters rpc.RPCParameters, config types.Configu
return core.NilHash, err
}

txnOpts := razorUtils.GetTxnOpts(rpcParameters, types.TransactionOptions{
txnOpts, err := razorUtils.GetTxnOpts(rpcParameters, types.TransactionOptions{
ChainId: core.ChainId,
Config: config,
ContractAddress: core.VoteManagerAddress,
Expand All @@ -160,6 +160,9 @@ func (*UtilsStruct) Commit(rpcParameters rpc.RPCParameters, config types.Configu
Parameters: []interface{}{epoch, commitmentToSend},
Account: account,
})
if err != nil {
return core.NilHash, err
}

log.Info("Commitment sent...")
client, err := rpcParameters.RPCManager.GetBestRPCClient()
Expand Down
22 changes: 16 additions & 6 deletions cmd/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ func TestCommit(t *testing.T) {
)

type args struct {
state int64
stateErr error
commitTxn *Types.Transaction
commitErr error
hash common.Hash
state int64
stateErr error
txnOptsErr error
commitTxn *Types.Transaction
commitErr error
hash common.Hash
}
tests := []struct {
name string
Expand Down Expand Up @@ -75,13 +76,22 @@ func TestCommit(t *testing.T) {
want: core.NilHash,
wantErr: errors.New("commit error"),
},
{
name: "Test 4: When there is an error in getting txnOpts",
args: args{
state: 0,
txnOptsErr: errors.New("txnOpts error"),
},
want: core.NilHash,
wantErr: errors.New("txnOpts error"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
SetUpMockInterfaces()

utilsMock.On("GetBufferedState", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.state, tt.args.stateErr)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts, tt.args.txnOptsErr)
voteManagerMock.On("Commit", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("*bind.TransactOpts"), mock.AnythingOfType("uint32"), mock.Anything).Return(tt.args.commitTxn, tt.args.commitErr)
transactionMock.On("Hash", mock.AnythingOfType("*types.Transaction")).Return(tt.args.hash)

Expand Down
5 changes: 4 additions & 1 deletion cmd/confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ func (*UtilsStruct) ClaimBlockReward(rpcParameters rpc.RPCParameters, options ty

if selectedProposedBlock.ProposerId == stakerID {
log.Info("Claiming block reward...")
txnOpts := razorUtils.GetTxnOpts(rpcParameters, options)
txnOpts, err := razorUtils.GetTxnOpts(rpcParameters, options)
if err != nil {
return core.NilHash, err
}

client, err := rpcParameters.RPCManager.GetBestRPCClient()
if err != nil {
Expand Down
18 changes: 16 additions & 2 deletions cmd/confirm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package cmd

import (
"errors"
"github.com/stretchr/testify/mock"
"math/big"
"razor/core"
"razor/core/types"
"razor/pkg/bindings"
"testing"

"github.com/stretchr/testify/mock"

"github.com/ethereum/go-ethereum/common"
Types "github.com/ethereum/go-ethereum/core/types"
)
Expand All @@ -25,6 +26,7 @@ func TestClaimBlockReward(t *testing.T) {
sortedProposedBlockIdsErr error
selectedBlock bindings.StructsBlock
selectedBlockErr error
txnOptsErr error
ClaimBlockRewardTxn *Types.Transaction
ClaimBlockRewardErr error
hash common.Hash
Expand Down Expand Up @@ -124,6 +126,18 @@ func TestClaimBlockReward(t *testing.T) {
want: core.NilHash,
wantErr: nil,
},
{
name: "Test 9: When there is an error in getting txnOpts",
args: args{
epoch: 5,
stakerId: 2,
sortedProposedBlockIds: []uint32{2, 1, 3},
selectedBlock: bindings.StructsBlock{ProposerId: 2},
txnOptsErr: errors.New("txnOpts error"),
},
want: core.NilHash,
wantErr: errors.New("txnOpts error"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -133,7 +147,7 @@ func TestClaimBlockReward(t *testing.T) {
utilsMock.On("GetSortedProposedBlockIds", mock.Anything, mock.Anything).Return(tt.args.sortedProposedBlockIds, tt.args.sortedProposedBlockIdsErr)
utilsMock.On("GetStakerId", mock.Anything, mock.Anything).Return(tt.args.stakerId, tt.args.stakerIdErr)
utilsMock.On("GetProposedBlock", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.selectedBlock, tt.args.selectedBlockErr)
utilsMock.On("GetTxnOpts", mock.Anything, options).Return(TxnOpts)
utilsMock.On("GetTxnOpts", mock.Anything, options).Return(TxnOpts, tt.args.txnOptsErr)
blockManagerMock.On("ClaimBlockReward", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("*bind.TransactOpts")).Return(tt.args.ClaimBlockRewardTxn, tt.args.ClaimBlockRewardErr)
transactionMock.On("Hash", mock.AnythingOfType("*types.Transaction")).Return(tt.args.hash)

Expand Down
5 changes: 4 additions & 1 deletion cmd/createCollection.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (*UtilsStruct) CreateCollection(rpcParameters rpc.RPCParameters, config typ
log.Error("Error in fetching state")
return core.NilHash, err
}
txnOpts := razorUtils.GetTxnOpts(rpcParameters, types.TransactionOptions{
txnOpts, err := razorUtils.GetTxnOpts(rpcParameters, types.TransactionOptions{
ChainId: core.ChainId,
Config: config,
ContractAddress: core.CollectionManagerAddress,
Expand All @@ -85,6 +85,9 @@ func (*UtilsStruct) CreateCollection(rpcParameters rpc.RPCParameters, config typ
ABI: bindings.CollectionManagerMetaData.ABI,
Account: collectionInput.Account,
})
if err != nil {
return core.NilHash, err
}

client, err := rpcParameters.RPCManager.GetBestRPCClient()
if err != nil {
Expand Down
12 changes: 11 additions & 1 deletion cmd/createCollection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestCreateCollection(t *testing.T) {
type args struct {
jobIdUint8 []uint16
waitForAppropriateStateErr error
txnOptsErr error
createCollectionTxn *Types.Transaction
createCollectionErr error
hash common.Hash
Expand Down Expand Up @@ -65,13 +66,22 @@ func TestCreateCollection(t *testing.T) {
want: core.NilHash,
wantErr: errors.New("createCollection error"),
},
{
name: "Test 4: When there is an error in getting txnOpts",
args: args{
jobIdUint8: []uint16{1, 2},
txnOptsErr: errors.New("txnOpts error"),
},
want: core.NilHash,
wantErr: errors.New("txnOpts error"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
SetUpMockInterfaces()

utilsMock.On("ConvertUintArrayToUint16Array", mock.Anything).Return(tt.args.jobIdUint8)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts, tt.args.txnOptsErr)
cmdUtilsMock.On("WaitForAppropriateState", mock.Anything, mock.Anything, mock.Anything).Return(WaitForDisputeOrConfirmStateStatus, tt.args.waitForAppropriateStateErr)
assetManagerMock.On("CreateCollection", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.createCollectionTxn, tt.args.createCollectionErr)
transactionMock.On("Hash", mock.Anything).Return(tt.args.hash)
Expand Down
5 changes: 4 additions & 1 deletion cmd/createJob.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ func (*UtilsStruct) CreateJob(rpcParameters rpc.RPCParameters, config types.Conf
Account: jobInput.Account,
}

txnOpts := razorUtils.GetTxnOpts(rpcParameters, txnArgs)
txnOpts, err := razorUtils.GetTxnOpts(rpcParameters, txnArgs)
if err != nil {
return core.NilHash, err
}
log.Info("Creating Job...")

client, err := rpcParameters.RPCManager.GetBestRPCClient()
Expand Down
11 changes: 10 additions & 1 deletion cmd/createJob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestCreateJob(t *testing.T) {
var config types.Configurations

type args struct {
txnOptsErr error
createJobTxn *Types.Transaction
createJobErr error
hash common.Hash
Expand Down Expand Up @@ -49,12 +50,20 @@ func TestCreateJob(t *testing.T) {
want: core.NilHash,
wantErr: errors.New("createJob error"),
},
{
name: "Test 3: When there is an error in getting txnOpts",
args: args{
txnOptsErr: errors.New("txnOpts error"),
},
want: core.NilHash,
wantErr: errors.New("txnOpts error"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
SetUpMockInterfaces()

utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts, tt.args.txnOptsErr)
assetManagerMock.On("CreateJob", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("*bind.TransactOpts"), mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.createJobTxn, tt.args.createJobErr)
transactionMock.On("Hash", mock.Anything).Return(tt.args.hash)

Expand Down
5 changes: 4 additions & 1 deletion cmd/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ func (*UtilsStruct) Delegate(rpcParameters rpc.RPCParameters, txnArgs types.Tran
txnArgs.MethodName = "delegate"
txnArgs.ABI = bindings.StakeManagerMetaData.ABI
txnArgs.Parameters = []interface{}{stakerId, txnArgs.Amount}
delegationTxnOpts := razorUtils.GetTxnOpts(rpcParameters, txnArgs)
delegationTxnOpts, err := razorUtils.GetTxnOpts(rpcParameters, txnArgs)
if err != nil {
return core.NilHash, err
}
log.Info("Sending Delegate transaction...")

client, err := rpcParameters.RPCManager.GetBestRPCClient()
Expand Down
Loading

0 comments on commit fd91fc4

Please sign in to comment.