Skip to content

Commit

Permalink
see pull request changes on #111
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Nguyen authored and Andrew Nguyen committed Jul 27, 2022
1 parent e66cf87 commit 38684af
Show file tree
Hide file tree
Showing 11 changed files with 357 additions and 912 deletions.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,11 @@ protogen_clean:
protogen_local:
$(eval proto_dir = "./shared/types/proto/")

protoc --go_opt=paths=source_relative -I=${proto_dir} -I=./shared/types/proto --go_out=./shared/types ./shared/types/proto/*.proto
protoc --go_opt=paths=source_relative -I=${proto_dir} -I=./utility/proto --go_out=./utility/types ./utility/proto/*.proto
protoc --go_opt=paths=source_relative -I=${proto_dir} -I=./shared/types/genesis/proto --go_out=./shared/types/genesis ./shared/types/genesis/proto/*.proto
protoc --go_opt=paths=source_relative -I=${proto_dir} -I=./consensus/types/proto --go_out=./consensus/types ./consensus/types/proto/*.proto
protoc --go_opt=paths=source_relative -I=${proto_dir} -I=./p2p/pre2p/raintree/types/proto --go_out=./p2p/pre2p/types ./p2p/pre2p/raintree/types/proto/*.proto
protoc --go_opt=paths=source_relative -I=${proto_dir} -I=./shared/types/proto --go_out=./shared/types ./shared/types/proto/*.proto --experimental_allow_proto3_optional
protoc --go_opt=paths=source_relative -I=${proto_dir} -I=./utility/proto --go_out=./utility/types ./utility/proto/*.proto --experimental_allow_proto3_optional
protoc --go_opt=paths=source_relative -I=${proto_dir} -I=./shared/types/genesis/proto --go_out=./shared/types/genesis ./shared/types/genesis/proto/*.proto --experimental_allow_proto3_optional
protoc --go_opt=paths=source_relative -I=${proto_dir} -I=./consensus/types/proto --go_out=./consensus/types ./consensus/types/proto/*.proto --experimental_allow_proto3_optional
protoc --go_opt=paths=source_relative -I=${proto_dir} -I=./p2p/pre2p/raintree/types/proto --go_out=./p2p/pre2p/types ./p2p/pre2p/raintree/types/proto/*.proto --experimental_allow_proto3_optional

echo "View generated proto files by running: make protogen_show"

Expand Down
4 changes: 0 additions & 4 deletions persistence/pre_persistence/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,6 @@ func TestUpdateApp(t *testing.T) {
if err != nil {
t.Fatal(err)
}
//before, err := ctx.(*PrePersistenceContext).GetApp(actor.Address, height)
//require.NoError(t, err)
//tokens := before.StakedTokens
//bigBeforeTokens, err := types.StringToBigInt(tokens)
require.NoError(t, err)
err = ctx.UpdateApp(actor.Address, zero, one, typesGenesis.DefaultChains)
require.NoError(t, err)
Expand Down
105 changes: 27 additions & 78 deletions shared/tests/utility_module/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utility_module

import (
"bytes"
"fmt"
"math/big"
"testing"

Expand All @@ -20,15 +21,11 @@ func TestUtilityContext_AddAccountAmount(t *testing.T) {
initialAmount, err := types.StringToBigInt(acc.Amount)
require.NoError(t, err)
addAmount := big.NewInt(1)
if err := ctx.AddAccountAmount(acc.Address, addAmount); err != nil {
t.Fatal(err)
}
require.NoError(t, ctx.AddAccountAmount(acc.Address, addAmount), "add account amount")
afterAmount, err := ctx.GetAccountAmount(acc.Address)
require.NoError(t, err)
expected := initialAmount.Add(initialAmount, addAmount)
if afterAmount.Cmp(expected) != 0 {
t.Fatalf("amounts are not equal, expected %v, got %v", initialAmount, afterAmount)
}
require.True(t, afterAmount.Cmp(expected) == 0, fmt.Sprintf("amounts are not equal, expected %v, got %v", initialAmount, afterAmount))
}

func TestUtilityContext_AddAccountAmountString(t *testing.T) {
Expand All @@ -38,15 +35,11 @@ func TestUtilityContext_AddAccountAmountString(t *testing.T) {
require.NoError(t, err)
addAmount := big.NewInt(1)
addAmountString := types.BigIntToString(addAmount)
if err := ctx.AddAccountAmountString(acc.Address, addAmountString); err != nil {
t.Fatal(err)
}
require.NoError(t, ctx.AddAccountAmountString(acc.Address, addAmountString), "add account amount string")
afterAmount, err := ctx.GetAccountAmount(acc.Address)
require.NoError(t, err)
expected := initialAmount.Add(initialAmount, addAmount)
if afterAmount.Cmp(expected) != 0 {
t.Fatalf("amounts are not equal, expected %v, got %v", initialAmount, afterAmount)
}
require.True(t, afterAmount.Cmp(expected) == 0, fmt.Sprintf("amounts are not equal, expected %v, got %v", initialAmount, afterAmount))
}

func TestUtilityContext_AddPoolAmount(t *testing.T) {
Expand All @@ -55,15 +48,11 @@ func TestUtilityContext_AddPoolAmount(t *testing.T) {
initialAmount, err := types.StringToBigInt(pool.Account.Amount)
require.NoError(t, err)
addAmount := big.NewInt(1)
if err := ctx.AddPoolAmount(pool.Name, addAmount); err != nil {
t.Fatal(err)
}
require.NoError(t, ctx.AddPoolAmount(pool.Name, addAmount), "add pool amount")
afterAmount, err := ctx.GetPoolAmount(pool.Name)
require.NoError(t, err)
expected := initialAmount.Add(initialAmount, addAmount)
if afterAmount.Cmp(expected) != 0 {
t.Fatalf("amounts are not equal, expected %v, got %v", initialAmount, afterAmount)
}
require.True(t, afterAmount.Cmp(expected) == 0, fmt.Sprintf("amounts are not equal, expected %v, got %v", initialAmount, afterAmount))
}

func TestUtilityContext_HandleMessageSend(t *testing.T) {
Expand All @@ -76,20 +65,14 @@ func TestUtilityContext_HandleMessageSend(t *testing.T) {
recipientBalanceBefore, err := types.StringToBigInt(accs[1].Amount)
require.NoError(t, err)
msg := NewTestingSendMessage(t, accs[0].Address, accs[1].Address, sendAmountString)
if err := ctx.HandleMessageSend(&msg); err != nil {
t.Fatal(err)
}
require.NoError(t, ctx.HandleMessageSend(&msg), "handle message send")
accs = GetAllTestingAccounts(t, ctx)
senderBalanceAfter, err := types.StringToBigInt(accs[0].Amount)
require.NoError(t, err)
recipientBalanceAfter, err := types.StringToBigInt(accs[1].Amount)
require.NoError(t, err)
if big.NewInt(0).Sub(senderBalanceBefore, senderBalanceAfter).Cmp(sendAmount) != 0 {
t.Fatal("unexpected sender balance")
}
if big.NewInt(0).Sub(recipientBalanceAfter, recipientBalanceBefore).Cmp(sendAmount) != 0 {
t.Fatal("unexpected recipient balance")
}
require.True(t, big.NewInt(0).Sub(senderBalanceBefore, senderBalanceAfter).Cmp(sendAmount) == 0, fmt.Sprintf("unexpected sender balance"))
require.True(t, big.NewInt(0).Sub(recipientBalanceAfter, recipientBalanceBefore).Cmp(sendAmount) == 0, fmt.Sprintf("unexpected recipient balance"))
}

func TestUtilityContext_GetMessageSendSignerCandidates(t *testing.T) {
Expand All @@ -100,57 +83,41 @@ func TestUtilityContext_GetMessageSendSignerCandidates(t *testing.T) {
msg := NewTestingSendMessage(t, accs[0].Address, accs[1].Address, sendAmountString)
candidates, err := ctx.GetMessageSendSignerCandidates(&msg)
require.NoError(t, err)
if len(candidates) != 1 {
t.Fatalf("wrong number of candidates, expected %d, got %d", 1, len(candidates))
}
if !bytes.Equal(candidates[0], accs[0].Address) {
t.Fatal("unexpected signer candidate")
}
require.True(t, len(candidates) == 1, fmt.Sprintf("wrong number of candidates, expected %d, got %d", 1, len(candidates)))
require.True(t, bytes.Equal(candidates[0], accs[0].Address), fmt.Sprintf("unexpected signer candidate"))
}

func TestUtilityContext_InsertPool(t *testing.T) {
ctx := NewTestingUtilityContext(t, 0)
testPoolName := "TEST_POOL"
addr, _ := crypto.GenerateAddress()
amount := types.BigIntToString(big.NewInt(1000))
if err := ctx.InsertPool(testPoolName, addr, amount); err != nil {
t.Fatal(err)
}
require.NoError(t, ctx.InsertPool(testPoolName, addr, amount), "insert pool")
gotAmount, err := ctx.GetPoolAmount(testPoolName)
require.NoError(t, err)
gotAmountString := types.BigIntToString(gotAmount)
if amount != gotAmountString {
t.Fatalf("unexpected amount, expected %s got %s", amount, gotAmountString)
}
require.True(t, amount == gotAmountString, fmt.Sprintf("unexpected amount, expected %s got %s", amount, gotAmountString))
}

func TestUtilityContext_SetAccountAmount(t *testing.T) {
ctx := NewTestingUtilityContext(t, 0)
addr, _ := crypto.GenerateAddress()
amount := big.NewInt(100)
if err := ctx.SetAccountAmount(addr, amount); err != nil {
t.Fatal(err)
}
require.NoError(t, ctx.SetAccountAmount(addr, amount), "set account amount")
gotAmount, err := ctx.GetAccountAmount(addr)
require.NoError(t, err)
if gotAmount.Cmp(amount) != 0 {
t.Fatalf("unexpected amounts: expected %v, got %v", amount, gotAmount)
}
require.True(t, gotAmount.Cmp(amount) == 0, fmt.Sprintf("unexpected amounts: expected %v, got %v", amount, gotAmount))
}

func TestUtilityContext_SetAccountWithAmountString(t *testing.T) {
ctx := NewTestingUtilityContext(t, 0)
addr, _ := crypto.GenerateAddress()
amount := big.NewInt(100)
amountString := types.BigIntToString(amount)
if err := ctx.SetAccountWithAmountString(addr, amountString); err != nil {
t.Fatal(err)
}
require.NoError(t, ctx.SetAccountWithAmountString(addr, amountString), "set account amount string")
gotAmount, err := ctx.GetAccountAmount(addr)
require.NoError(t, err)
if gotAmount.Cmp(amount) != 0 {
t.Fatalf("unexpected amounts: expected %v, got %v", amount, gotAmount)
}
require.True(t, gotAmount.Cmp(amount) == 0, fmt.Sprintf("unexpected amounts: expected %v, got %v", amount, gotAmount))
}

func TestUtilityContext_SetPoolAmount(t *testing.T) {
Expand All @@ -160,17 +127,11 @@ func TestUtilityContext_SetPoolAmount(t *testing.T) {
beforeAmountBig, err := types.StringToBigInt(beforeAmount)
require.NoError(t, err)
expectedAfterAmount := big.NewInt(100)
if err := ctx.SetPoolAmount(pool.Name, expectedAfterAmount); err != nil {
t.Fatal(err)
}
require.NoError(t, ctx.SetPoolAmount(pool.Name, expectedAfterAmount), "set pool amount")
amount, err := ctx.GetPoolAmount(pool.Name)
require.NoError(t, err)
if beforeAmountBig.Cmp(amount) == 0 {
t.Fatal("no amount change in pool")
}
if expectedAfterAmount.Cmp(amount) != 0 {
t.Fatalf("unexpected pool amount; expected %v got %v", expectedAfterAmount, amount)
}
require.True(t, beforeAmountBig.Cmp(amount) != 0, fmt.Sprintf("no amount change in pool"))
require.True(t, expectedAfterAmount.Cmp(amount) == 0, fmt.Sprintf("unexpected pool amount; expected %v got %v", expectedAfterAmount, amount))
}

func TestUtilityContext_SubPoolAmount(t *testing.T) {
Expand All @@ -180,18 +141,12 @@ func TestUtilityContext_SubPoolAmount(t *testing.T) {
ctx.SetPoolAmount(pool.Name, beforeAmountBig)
subAmountBig := big.NewInt(100)
subAmount := types.BigIntToString(subAmountBig)
if err := ctx.SubPoolAmount(pool.Name, subAmount); err != nil {
t.Fatal(err)
}
require.NoError(t, ctx.SubPoolAmount(pool.Name, subAmount), "sub pool amount")
amount, err := ctx.GetPoolAmount(pool.Name)
require.NoError(t, err)
if beforeAmountBig.Cmp(amount) == 0 {
t.Fatal("no amount change in pool")
}
require.True(t, beforeAmountBig.Cmp(amount) != 0, fmt.Sprintf("no amount change in pool"))
expected := beforeAmountBig.Sub(beforeAmountBig, subAmountBig)
if expected.Cmp(amount) != 0 {
t.Fatalf("unexpected pool amount; expected %v got %v", expected, amount)
}
require.True(t, expected.Cmp(amount) == 0, fmt.Sprintf("unexpected pool amount; expected %v got %v", expected, amount))
}

func TestUtilityContext_SubtractAccountAmount(t *testing.T) {
Expand All @@ -201,18 +156,12 @@ func TestUtilityContext_SubtractAccountAmount(t *testing.T) {
beforeAmountBig, err := types.StringToBigInt(beforeAmount)
require.NoError(t, err)
subAmountBig := big.NewInt(100)
if err := ctx.SubtractAccountAmount(acc.Address, subAmountBig); err != nil {
t.Fatal(err)
}
require.NoError(t, ctx.SubtractAccountAmount(acc.Address, subAmountBig), "sub account amount")
amount, err := ctx.GetAccountAmount(acc.Address)
require.NoError(t, err)
if beforeAmountBig.Cmp(amount) == 0 {
t.Fatal("no amount change in pool")
}
require.True(t, beforeAmountBig.Cmp(amount) != 0, fmt.Sprintf("no amount change in pool"))
expected := beforeAmountBig.Sub(beforeAmountBig, subAmountBig)
if expected.Cmp(amount) != 0 {
t.Fatalf("unexpected acc amount; expected %v got %v", expected, amount)
}
require.True(t, expected.Cmp(amount) == 0, fmt.Sprintf("unexpected acc amount; expected %v got %v", expected, amount))
}

func GetAllTestingAccounts(t *testing.T, ctx utility.UtilityContext) []*genesis.Account {
Expand Down
Loading

0 comments on commit 38684af

Please sign in to comment.