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

feat: TotalCoin and TotalCoinKeeper implementation #2671

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4593a4c
Implement total coins (#8)
linhpn99 Aug 9, 2024
56983b4
fixup
linhpn99 Aug 10, 2024
092d036
update unit tests, dont export SetCoins
linhpn99 Aug 10, 2024
95a37fb
fix failure CIs
linhpn99 Aug 10, 2024
2c86f09
fixup
linhpn99 Aug 10, 2024
d7da29e
Merge branch 'master' into dev-thinhnx/feat_gno_implement_total_coin
linhpn99 Aug 16, 2024
674244a
improve test coverage
linhpn99 Aug 16, 2024
7c09c31
Merge branch 'dev-thinhnx/feat_gno_implement_total_coin' of https://g…
linhpn99 Aug 16, 2024
f4d6fc0
fixup
linhpn99 Aug 16, 2024
d1362b1
run test parallel
linhpn99 Aug 16, 2024
12fa826
fixup
linhpn99 Aug 16, 2024
7b2e8c2
Merge branch 'master' into dev-thinhnx/feat_gno_implement_total_coin
thinhnx-var Aug 20, 2024
1df8e93
resolve conflict
linhpn99 Aug 20, 2024
4fe3902
AddCoins() instead of SetCoins()
linhpn99 Aug 20, 2024
6c1934a
change SetCoins to internal function
linhpn99 Aug 20, 2024
7972646
add test
linhpn99 Aug 24, 2024
043ccbc
Merge branch 'master' into dev-thinhnx/feat_gno_implement_total_coin
linhpn99 Aug 24, 2024
d32ed5f
add check overflow/underflow
linhpn99 Aug 30, 2024
33b5d16
fixup
linhpn99 Aug 30, 2024
0c96998
Merge branch 'master' into dev-thinhnx/feat_gno_implement_total_coin
linhpn99 Aug 30, 2024
1c9b0dc
Update file.go
linhpn99 Sep 2, 2024
8af0df7
Merge branch 'master' into dev-thinhnx/feat_gno_implement_total_coin
linhpn99 Sep 3, 2024
ba3dcfa
Merge branch 'master' into dev-thinhnx/feat_gno_implement_total_coin
linhpn99 Sep 4, 2024
4b5decd
merge master
linhpn99 Sep 11, 2024
cc22e45
fix txtar
linhpn99 Sep 11, 2024
0f1fcd6
rename file
linhpn99 Sep 11, 2024
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
4 changes: 2 additions & 2 deletions gno.land/cmd/gnoland/testdata/restart_missing_type.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ gnokey sign -tx-path $WORK/tx1.tx -chainid tendermint_test -account-sequence 0 t
! gnokey broadcast $WORK/tx1.tx
stderr 'out of gas'

gnokey sign -tx-path $WORK/tx2.tx -chainid tendermint_test -account-sequence 1 test1
gnokey sign -tx-path $WORK/tx2.tx -chainid tendermint_test -account-sequence 0 test1
gnokey broadcast $WORK/tx2.tx
stdout 'OK!'

gnokey sign -tx-path $WORK/tx3.tx -chainid tendermint_test -account-sequence 2 test1
gnokey sign -tx-path $WORK/tx3.tx -chainid tendermint_test -account-sequence 1 test1
gnokey broadcast $WORK/tx3.tx
stdout 'OK!'

Expand Down
5 changes: 3 additions & 2 deletions gno.land/pkg/gnoland/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ func NewAppWithOptions(cfg *AppOptions) (abci.Application, error) {

// Construct keepers.
acctKpr := auth.NewAccountKeeper(mainKey, ProtoGnoAccount)
bankKpr := bank.NewBankKeeper(acctKpr)
tckpr := bank.NewTotalCoinKeeper(mainKey)
bankKpr := bank.NewBankKeeper(acctKpr, tckpr)
vmk := vm.NewVMKeeper(baseKey, mainKey, acctKpr, bankKpr, cfg.MaxCycles)

// Set InitChainer
Expand Down Expand Up @@ -291,7 +292,7 @@ func (cfg InitChainerConfig) loadAppState(ctx sdk.Context, appState any) ([]abci
for _, bal := range state.Balances {
acc := cfg.acctKpr.NewAccountWithAddress(ctx, bal.Address)
cfg.acctKpr.SetAccount(ctx, acc)
err := cfg.bankKpr.SetCoins(ctx, bal.Address, bal.Amount)
_, err := cfg.bankKpr.AddCoins(ctx, bal.Address, bal.Amount)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion gno.land/pkg/sdk/vm/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
}

func (bnk *SDKBanker) TotalCoin(denom string) int64 {
panic("not yet implemented")
return bnk.vmk.bank.TotalCoin(bnk.ctx, denom)

Check warning on line 40 in gno.land/pkg/sdk/vm/builtins.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/builtins.go#L40

Added line #L40 was not covered by tests
}

func (bnk *SDKBanker) IssueCoin(b32addr crypto.Bech32Address, denom string, amount int64) {
Expand Down
3 changes: 2 additions & 1 deletion gno.land/pkg/sdk/vm/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ func _setupTestEnv(cacheStdlibs bool) testEnv {

ctx := sdk.NewContext(sdk.RunTxModeDeliver, ms, &bft.Header{ChainID: "test-chain-id"}, log.NewNoopLogger())
acck := authm.NewAccountKeeper(iavlCapKey, std.ProtoBaseAccount)
bank := bankm.NewBankKeeper(acck)
tck := bankm.NewTotalCoinKeeper(iavlCapKey)
bank := bankm.NewBankKeeper(acck, tck)
vmk := NewVMKeeper(baseCapKey, iavlCapKey, acck, bank, 100_000_000)

mcw := ms.MultiCacheWrap()
Expand Down
2 changes: 1 addition & 1 deletion gno.land/pkg/sdk/vm/gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func setupAddPkg(success bool) (sdk.Context, sdk.Tx, vmHandler) {
addr := crypto.AddressFromPreimage([]byte("test1"))
acc := env.acck.NewAccountWithAddress(ctx, addr)
env.acck.SetAccount(ctx, acc)
env.bank.SetCoins(ctx, addr, std.MustParseCoins(ugnot.ValueString(10000000)))
env.bank.AddCoins(ctx, addr, std.MustParseCoins(ugnot.ValueString(10000000)))
// success message
var files []*std.MemFile
if success {
Expand Down
22 changes: 10 additions & 12 deletions gno.land/pkg/sdk/vm/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package vm
import (
"errors"
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -31,7 +30,7 @@ func TestVMKeeperAddPackage(t *testing.T) {
addr := crypto.AddressFromPreimage([]byte("addr1"))
acc := env.acck.NewAccountWithAddress(ctx, addr)
env.acck.SetAccount(ctx, acc)
env.bank.SetCoins(ctx, addr, std.MustParseCoins(coinsString))
env.bank.AddCoins(ctx, addr, std.MustParseCoins(coinsString))
assert.True(t, env.bank.GetCoins(ctx, addr).IsEqual(std.MustParseCoins(coinsString)))

// Create test package.
Expand Down Expand Up @@ -76,7 +75,7 @@ func TestVMKeeperOrigSend1(t *testing.T) {
addr := crypto.AddressFromPreimage([]byte("addr1"))
acc := env.acck.NewAccountWithAddress(ctx, addr)
env.acck.SetAccount(ctx, acc)
env.bank.SetCoins(ctx, addr, std.MustParseCoins(coinsString))
env.bank.AddCoins(ctx, addr, std.MustParseCoins(coinsString))
assert.True(t, env.bank.GetCoins(ctx, addr).IsEqual(std.MustParseCoins(coinsString)))

// Create test package.
Expand Down Expand Up @@ -121,7 +120,7 @@ func TestVMKeeperOrigSend2(t *testing.T) {
addr := crypto.AddressFromPreimage([]byte("addr1"))
acc := env.acck.NewAccountWithAddress(ctx, addr)
env.acck.SetAccount(ctx, acc)
env.bank.SetCoins(ctx, addr, std.MustParseCoins(coinsString))
env.bank.AddCoins(ctx, addr, std.MustParseCoins(coinsString))
assert.True(t, env.bank.GetCoins(ctx, addr).IsEqual(std.MustParseCoins(coinsString)))

// Create test package.
Expand Down Expand Up @@ -162,8 +161,7 @@ func GetAdmin() string {
res, err := env.vmk.Call(ctx, msg2)
assert.Error(t, err)
assert.Equal(t, "", res)
fmt.Println(err.Error())
assert.True(t, strings.Contains(err.Error(), "insufficient coins error"))
assert.Equal(t, err.Error(), "invalid coins error")
}

// Sending more than tx send fails.
Expand All @@ -175,7 +173,7 @@ func TestVMKeeperOrigSend3(t *testing.T) {
addr := crypto.AddressFromPreimage([]byte("addr1"))
acc := env.acck.NewAccountWithAddress(ctx, addr)
env.acck.SetAccount(ctx, acc)
env.bank.SetCoins(ctx, addr, std.MustParseCoins(coinsString))
env.bank.AddCoins(ctx, addr, std.MustParseCoins(coinsString))
assert.True(t, env.bank.GetCoins(ctx, addr).IsEqual(std.MustParseCoins(coinsString)))

// Create test package.
Expand Down Expand Up @@ -219,7 +217,7 @@ func TestVMKeeperRealmSend1(t *testing.T) {
addr := crypto.AddressFromPreimage([]byte("addr1"))
acc := env.acck.NewAccountWithAddress(ctx, addr)
env.acck.SetAccount(ctx, acc)
env.bank.SetCoins(ctx, addr, std.MustParseCoins(coinsString))
env.bank.AddCoins(ctx, addr, std.MustParseCoins(coinsString))
assert.True(t, env.bank.GetCoins(ctx, addr).IsEqual(std.MustParseCoins(coinsString)))

// Create test package.
Expand Down Expand Up @@ -263,7 +261,7 @@ func TestVMKeeperRealmSend2(t *testing.T) {
addr := crypto.AddressFromPreimage([]byte("addr1"))
acc := env.acck.NewAccountWithAddress(ctx, addr)
env.acck.SetAccount(ctx, acc)
env.bank.SetCoins(ctx, addr, std.MustParseCoins(coinsString))
env.bank.AddCoins(ctx, addr, std.MustParseCoins(coinsString))
assert.True(t, env.bank.GetCoins(ctx, addr).IsEqual(std.MustParseCoins(coinsString)))

// Create test package.
Expand Down Expand Up @@ -307,7 +305,7 @@ func TestVMKeeperOrigCallerInit(t *testing.T) {
addr := crypto.AddressFromPreimage([]byte("addr1"))
acc := env.acck.NewAccountWithAddress(ctx, addr)
env.acck.SetAccount(ctx, acc)
env.bank.SetCoins(ctx, addr, std.MustParseCoins(coinsString))
env.bank.AddCoins(ctx, addr, std.MustParseCoins(coinsString))
assert.True(t, env.bank.GetCoins(ctx, addr).IsEqual(std.MustParseCoins(coinsString)))

// Create test package.
Expand Down Expand Up @@ -430,7 +428,7 @@ func TestNumberOfArgsError(t *testing.T) {
addr := crypto.AddressFromPreimage([]byte("addr1"))
acc := env.acck.NewAccountWithAddress(ctx, addr)
env.acck.SetAccount(ctx, acc)
env.bank.SetCoins(ctx, addr, std.MustParseCoins(coinsString))
env.bank.AddCoins(ctx, addr, std.MustParseCoins(coinsString))
assert.True(t, env.bank.GetCoins(ctx, addr).IsEqual(std.MustParseCoins(coinsString)))

// Create test package.
Expand Down Expand Up @@ -469,7 +467,7 @@ func TestVMKeeperReinitialize(t *testing.T) {
addr := crypto.AddressFromPreimage([]byte("addr1"))
acc := env.acck.NewAccountWithAddress(ctx, addr)
env.acck.SetAccount(ctx, acc)
env.bank.SetCoins(ctx, addr, std.MustParseCoins(coinsString))
env.bank.AddCoins(ctx, addr, std.MustParseCoins(coinsString))
assert.True(t, env.bank.GetCoins(ctx, addr).IsEqual(std.MustParseCoins(coinsString)))

// Create test package.
Expand Down
29 changes: 23 additions & 6 deletions gnovm/tests/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
osm "github.com/gnolang/gno/tm2/pkg/os"
"github.com/gnolang/gno/tm2/pkg/sdk"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/gnolang/overflow"
"github.com/pmezard/go-difflib/difflib"
)

Expand Down Expand Up @@ -548,20 +549,28 @@

type testBanker struct {
coinTable map[crypto.Bech32Address]std.Coins
totalCoin map[string]int64
}

func newTestBanker(args ...interface{}) *testBanker {
coinTable := make(map[crypto.Bech32Address]std.Coins)
totalCoin := make(map[string]int64)
if len(args)%2 != 0 {
panic("newTestBanker requires even number of arguments; addr followed by coins")
}
for i := 0; i < len(args); i += 2 {
addr := args[i].(crypto.Bech32Address)
amount := args[i+1].(std.Coins)
coinTable[addr] = amount
coins := args[i+1].(std.Coins)
coinTable[addr] = coins
for _, coin := range coins {
if coin.IsValid() {
totalCoin[coin.Denom] += coin.Amount
}
}
}
return &testBanker{
coinTable: coinTable,
totalCoin: totalCoin,
}
}

Expand All @@ -586,23 +595,31 @@
tb.coinTable[from] = frest
// Second, add to 'to'.
// NOTE: even works when from==to, due to 2-step isolation.
tcoins, _ := tb.coinTable[to]
tcoins := tb.coinTable[to]

Check warning on line 598 in gnovm/tests/file.go

View check run for this annotation

Codecov / codecov/patch

gnovm/tests/file.go#L598

Added line #L598 was not covered by tests
tsum := tcoins.Add(amt)
tb.coinTable[to] = tsum
}

func (tb *testBanker) TotalCoin(denom string) int64 {
panic("not yet implemented")
return tb.totalCoin[denom]

Check warning on line 604 in gnovm/tests/file.go

View check run for this annotation

Codecov / codecov/patch

gnovm/tests/file.go#L604

Added line #L604 was not covered by tests
}

func (tb *testBanker) IssueCoin(addr crypto.Bech32Address, denom string, amt int64) {
coins, _ := tb.coinTable[addr]
coins := tb.coinTable[addr]

Check warning on line 608 in gnovm/tests/file.go

View check run for this annotation

Codecov / codecov/patch

gnovm/tests/file.go#L608

Added line #L608 was not covered by tests
sum := coins.Add(std.Coins{{denom, amt}})
tb.coinTable[addr] = sum

totalCoin := overflow.Add64p(tb.totalCoin[denom], amt)

Check warning on line 612 in gnovm/tests/file.go

View check run for this annotation

Codecov / codecov/patch

gnovm/tests/file.go#L612

Added line #L612 was not covered by tests

tb.totalCoin[denom] = totalCoin

Check warning on line 614 in gnovm/tests/file.go

View check run for this annotation

Codecov / codecov/patch

gnovm/tests/file.go#L614

Added line #L614 was not covered by tests
}

func (tb *testBanker) RemoveCoin(addr crypto.Bech32Address, denom string, amt int64) {
coins, _ := tb.coinTable[addr]
coins := tb.coinTable[addr]

Check warning on line 618 in gnovm/tests/file.go

View check run for this annotation

Codecov / codecov/patch

gnovm/tests/file.go#L618

Added line #L618 was not covered by tests
rest := coins.Sub(std.Coins{{denom, amt}})
tb.coinTable[addr] = rest

totalCoin := overflow.Sub64p(tb.totalCoin[denom], amt)

Check warning on line 622 in gnovm/tests/file.go

View check run for this annotation

Codecov / codecov/patch

gnovm/tests/file.go#L622

Added line #L622 was not covered by tests

tb.totalCoin[denom] = totalCoin

Check warning on line 624 in gnovm/tests/file.go

View check run for this annotation

Codecov / codecov/patch

gnovm/tests/file.go#L624

Added line #L624 was not covered by tests
}
5 changes: 4 additions & 1 deletion tm2/pkg/sdk/bank/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/gnolang/gno/tm2/pkg/sdk"
"github.com/gnolang/gno/tm2/pkg/sdk/auth"

"github.com/gnolang/gno/tm2/pkg/std"
"github.com/gnolang/gno/tm2/pkg/store"
"github.com/gnolang/gno/tm2/pkg/store/iavl"
Expand All @@ -18,6 +19,7 @@ type testEnv struct {
ctx sdk.Context
bank BankKeeper
acck auth.AccountKeeper
tck TotalCoinKeeper
}

func setupTestEnv() testEnv {
Expand All @@ -34,7 +36,8 @@ func setupTestEnv() testEnv {
authCapKey, std.ProtoBaseAccount,
)

bank := NewBankKeeper(acck)
tck := NewTotalCoinKeeper(authCapKey)
bank := NewBankKeeper(acck, tck)

return testEnv{ctx: ctx, bank: bank, acck: acck}
}
8 changes: 8 additions & 0 deletions tm2/pkg/sdk/bank/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ package bank

const (
ModuleName = "bank"

// TotalCoinStoreKeyPrefix prefix for total-coin-by-denom store
TotalCoinStoreKeyPrefix = "/tc/"
)

// TotalCoinStoreKey turn an denom to key used to get it from the total coin store
func TotalCoinStoreKey(denom string) []byte {
return []byte(TotalCoinStoreKeyPrefix + denom)
}
Loading
Loading