refactor: tests and structure (#3)

module:
- cic-go

packages:
- cic/meta
- cic/net

other fixes:

- nonce is now a settable value
- package init outside test runner scope
This commit is contained in:
Mohamed Sohail 2022-05-09 10:58:04 +03:00 committed by GitHub
parent 7731a23e35
commit 79634affb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 82 additions and 74 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
covprofile covprofile
*.env

2
go.mod
View File

@ -1,4 +1,4 @@
module github.com/grassrootseconomics/cic_go module github.com/grassrootseconomics/cic-go
go 1.18 go 1.18

View File

@ -1,4 +1,4 @@
package cic_meta package meta
import ( import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"

View File

@ -1,4 +1,4 @@
package cic_meta package meta
import ( import (
"crypto/sha256" "crypto/sha256"

View File

@ -1,4 +1,4 @@
package cic_net package net
import ( import (
"crypto/ecdsa" "crypto/ecdsa"
@ -22,6 +22,7 @@ type WriteTx struct {
from common.Address from common.Address
to common.Address to common.Address
gasLimit uint64 gasLimit uint64
nonce uint64
privateKey ecdsa.PrivateKey privateKey ecdsa.PrivateKey
} }

View File

@ -1,4 +1,4 @@
package cic_net package net
import ( import (
"github.com/lmittmann/w3" "github.com/lmittmann/w3"
@ -22,9 +22,9 @@ func TestCicNet_Connect(t *testing.T) {
name := "Test RPC connection" name := "Test RPC connection"
wantErr := false wantErr := false
t.Run(name, func(t *testing.T) { cicnet, _ := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
cicnet, _ := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
t.Run(name, func(t *testing.T) {
if err := cicnet.Close(); (err != nil) != wantErr { if err := cicnet.Close(); (err != nil) != wantErr {
t.Errorf("EntryCount() error = %v, wantErr %v", err, wantErr) t.Errorf("EntryCount() error = %v, wantErr %v", err, wantErr)
} }

View File

@ -1,4 +1,4 @@
package cic_net package net
import ( import (
"context" "context"

View File

@ -1,4 +1,4 @@
package cic_net package net
import ( import (
"context" "context"
@ -44,16 +44,16 @@ func TestCicNet_DemurrageToken_DemurrageTokeInfo(t *testing.T) {
}, },
} }
cicnet, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
if err != nil {
t.Fatalf("NewCicNet error = %v", err)
}
for _, testcase := range tests { for _, testcase := range tests {
tt := testcase tt := testcase
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
cicnet, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
if err != nil {
t.Fatalf("NewCicNet error = %v", err)
}
got, err := cicnet.DemurrageTokenInfo(context.Background(), tt.args.contractAddress) got, err := cicnet.DemurrageTokenInfo(context.Background(), tt.args.contractAddress)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
@ -62,7 +62,7 @@ func TestCicNet_DemurrageToken_DemurrageTokeInfo(t *testing.T) {
if !tt.wantErr { if !tt.wantErr {
if got.DemurrageAmount.Cmp(big.NewInt(0)) < 1 { if got.DemurrageAmount.Cmp(big.NewInt(0)) < 1 {
t.Fatalf("DemurrageAmount = %v, want %d atleast", got, 1) t.Errorf("DemurrageAmount = %v, want %d atleast", got, 1)
} }
} }
}) })
@ -109,16 +109,16 @@ func TestCicNet_DemurrageToken_BaseBalanceOf(t *testing.T) {
}, },
} }
cicnet, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
if err != nil {
t.Fatalf("NewCicNet error = %v", err)
}
for _, testcase := range tests { for _, testcase := range tests {
tt := testcase tt := testcase
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
cicnet, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
if err != nil {
t.Fatalf("NewCicNet error = %v", err)
}
got, err := cicnet.BaseBalanceOf(context.Background(), tt.args.contractAddress, tt.args.accountAddress) got, err := cicnet.BaseBalanceOf(context.Background(), tt.args.contractAddress, tt.args.accountAddress)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
@ -127,7 +127,7 @@ func TestCicNet_DemurrageToken_BaseBalanceOf(t *testing.T) {
if !tt.wantErr { if !tt.wantErr {
if got.Cmp(&tt.balanceGte) < 0 { if got.Cmp(&tt.balanceGte) < 0 {
t.Fatalf("Token = %v, want %d", got, tt.balanceGte.Int64()) t.Errorf("Token = %v, want %d", got, tt.balanceGte.Int64())
} }
} }
}) })
@ -151,6 +151,16 @@ func TestCicNet_DemurrageToken_ChangePeriod(t *testing.T) {
} }
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA) fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
cicnet, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
if err != nil {
t.Fatalf("NewCicNet error = %v", err)
}
nonce, err := cicnet.LastNonce(context.Background(), fromAddress)
if err != nil {
t.Fatalf("Cicnet client error")
}
tests := []struct { tests := []struct {
name string name string
args args args args
@ -163,6 +173,7 @@ func TestCicNet_DemurrageToken_ChangePeriod(t *testing.T) {
writeTx: WriteTx{ writeTx: WriteTx{
from: fromAddress, from: fromAddress,
to: w3.A("0xaB89822F31c2092861F713F6F34bd6877a8C1878"), to: w3.A("0xaB89822F31c2092861F713F6F34bd6877a8C1878"),
nonce: nonce + 1,
gasLimit: 12000000, gasLimit: 12000000,
privateKey: *privateKey, privateKey: *privateKey,
}, },
@ -175,11 +186,6 @@ func TestCicNet_DemurrageToken_ChangePeriod(t *testing.T) {
tt := testcase tt := testcase
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
cicnet, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
if err != nil {
t.Fatalf("NewCicNet error = %v", err)
}
tx, err := cicnet.ChangePeriod(context.Background(), tt.args.writeTx) tx, err := cicnet.ChangePeriod(context.Background(), tt.args.writeTx)
t.Logf("ChangePeriod tx_hash %s", tx.String()) t.Logf("ChangePeriod tx_hash %s", tx.String())
@ -209,6 +215,16 @@ func TestCicNet_DemurrageToken_ApplyDemurrageLimited(t *testing.T) {
} }
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA) fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
cicnet, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
if err != nil {
t.Fatalf("NewCicNet error = %v", err)
}
nonce, err := cicnet.LastNonce(context.Background(), fromAddress)
if err != nil {
t.Fatalf("Cicnet client error")
}
tests := []struct { tests := []struct {
name string name string
args args args args
@ -222,6 +238,7 @@ func TestCicNet_DemurrageToken_ApplyDemurrageLimited(t *testing.T) {
writeTx: WriteTx{ writeTx: WriteTx{
from: fromAddress, from: fromAddress,
to: w3.A("0xaB89822F31c2092861F713F6F34bd6877a8C1878"), to: w3.A("0xaB89822F31c2092861F713F6F34bd6877a8C1878"),
nonce: nonce + 1,
gasLimit: 12000000, gasLimit: 12000000,
privateKey: *privateKey, privateKey: *privateKey,
}, },
@ -234,12 +251,6 @@ func TestCicNet_DemurrageToken_ApplyDemurrageLimited(t *testing.T) {
tt := testcase tt := testcase
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
cicnet, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
if err != nil {
t.Fatalf("NewCicNet error = %v", err)
}
tx, err := cicnet.ApplyDemurrageLimited(context.Background(), tt.args.rounds, tt.args.writeTx) tx, err := cicnet.ApplyDemurrageLimited(context.Background(), tt.args.rounds, tt.args.writeTx)
t.Logf("ApplyDemurrageLimited tx_hash %s", tx.String()) t.Logf("ApplyDemurrageLimited tx_hash %s", tx.String())

View File

@ -1,4 +1,4 @@
package cic_net package net
import ( import (
"context" "context"

View File

@ -1,4 +1,4 @@
package cic_net package net
import ( import (
"context" "context"
@ -37,16 +37,16 @@ func TestCicNet_ERC20Token_ERC20TokenInfo(t *testing.T) {
}, },
} }
cicnet, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
if err != nil {
t.Fatalf("NewCicNet error = %v", err)
}
for _, testcase := range tests { for _, testcase := range tests {
tt := testcase tt := testcase
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
cicnet, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
if err != nil {
t.Fatalf("NewCicNet error = %v", err)
}
got, err := cicnet.ERC20TokenInfo(context.Background(), tt.args.contractAddress) got, err := cicnet.ERC20TokenInfo(context.Background(), tt.args.contractAddress)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
@ -54,7 +54,7 @@ func TestCicNet_ERC20Token_ERC20TokenInfo(t *testing.T) {
} }
if got.Symbol != tt.symbol { if got.Symbol != tt.symbol {
t.Fatalf("Token = %v, want %v", got, tt.symbol) t.Errorf("Token = %v, want %v", got, tt.symbol)
} }
}) })
} }
@ -92,16 +92,16 @@ func TestCicNet_ERC20Token_BalanceOf(t *testing.T) {
}, },
} }
cicnet, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
if err != nil {
t.Fatalf("NewCicNet error = %v", err)
}
for _, testcase := range tests { for _, testcase := range tests {
tt := testcase tt := testcase
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
cicnet, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
if err != nil {
t.Fatalf("NewCicNet error = %v", err)
}
got, err := cicnet.BalanceOf(context.Background(), tt.args.contractAddress, tt.args.accountAddress) got, err := cicnet.BalanceOf(context.Background(), tt.args.contractAddress, tt.args.accountAddress)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
@ -109,7 +109,7 @@ func TestCicNet_ERC20Token_BalanceOf(t *testing.T) {
} }
if got.Cmp(&tt.balanceGte) < 0 { if got.Cmp(&tt.balanceGte) < 0 {
t.Fatalf("Token = %v, want %d", got, tt.balanceGte.Int64()) t.Errorf("Token = %v, want %d", got, tt.balanceGte.Int64())
} }
}) })
} }

View File

@ -1,4 +1,4 @@
package cic_net package net
import ( import (
"context" "context"

View File

@ -1,4 +1,4 @@
package cic_net package net
import ( import (
"context" "context"
@ -11,13 +11,13 @@ func TestCicNet_TokenIndex_EntryCount(t *testing.T) {
name := "Entry count" name := "Entry count"
wantErr := false wantErr := false
tokenIndex, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
if err != nil {
t.Fatalf("NewCicNet error = %v", err)
}
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
tokenIndex, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
if err != nil {
t.Fatalf("NewCicNet error = %v", err)
}
got, err := tokenIndex.EntryCount(context.Background()) got, err := tokenIndex.EntryCount(context.Background())
if (err != nil) != wantErr { if (err != nil) != wantErr {
@ -25,7 +25,7 @@ func TestCicNet_TokenIndex_EntryCount(t *testing.T) {
} }
if got.Cmp(big.NewInt(0)) < 1 { if got.Cmp(big.NewInt(0)) < 1 {
t.Fatalf("EntryCount() = %v, want %v", got, 1) t.Errorf("EntryCount() = %v, want %v", got, 1)
} }
}) })
} }
@ -59,16 +59,16 @@ func TestCicNet_TokenIndex_AddressAtIndex(t *testing.T) {
}, },
} }
tokenIndex, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
if err != nil {
t.Fatalf("NewCicNet error = %v", err)
}
for _, testcase := range tests { for _, testcase := range tests {
tt := testcase tt := testcase
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
tokenIndex, err := NewCicNet(conf.rpcProvider, w3.A(conf.tokenIndex))
if err != nil {
t.Fatalf("NewCicNet error = %v", err)
}
got, err := tokenIndex.AddressAtIndex(context.Background(), tt.args.index) got, err := tokenIndex.AddressAtIndex(context.Background(), tt.args.index)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
@ -76,7 +76,7 @@ func TestCicNet_TokenIndex_AddressAtIndex(t *testing.T) {
} }
if got != tt.address { if got != tt.address {
t.Fatalf("AddressAtIndex = %v, want %v", got, tt.address) t.Errorf("AddressAtIndex = %v, want %v", got, tt.address)
} }
}) })
} }

View File

@ -1,4 +1,4 @@
package cic_net package net
import ( import (
"context" "context"
@ -8,7 +8,7 @@ import (
"math/big" "math/big"
) )
func (c *CicNet) latestNonce(ctx context.Context, address common.Address) (uint64, error) { func (c *CicNet) LastNonce(ctx context.Context, address common.Address) (uint64, error) {
var nonce uint64 var nonce uint64
err := c.ethClient.CallCtx( err := c.ethClient.CallCtx(
@ -25,14 +25,9 @@ func (c *CicNet) latestNonce(ctx context.Context, address common.Address) (uint6
func (c *CicNet) signAndCall(ctx context.Context, input []byte, txData WriteTx) (common.Hash, error) { func (c *CicNet) signAndCall(ctx context.Context, input []byte, txData WriteTx) (common.Hash, error) {
var txHash common.Hash var txHash common.Hash
nonce, err := c.latestNonce(ctx, txData.from)
if err != nil {
return [32]byte{}, err
}
tx, err := types.SignNewTx(&txData.privateKey, c.kitabuSigner, &types.LegacyTx{ tx, err := types.SignNewTx(&txData.privateKey, c.kitabuSigner, &types.LegacyTx{
To: &txData.to, To: &txData.to,
Nonce: nonce, Nonce: txData.nonce,
Data: input, Data: input,
Gas: txData.gasLimit, Gas: txData.gasLimit,
GasPrice: big.NewInt(1), GasPrice: big.NewInt(1),