Skip to content

Commit

Permalink
feat: removing address from account (#454)
Browse files Browse the repository at this point in the history
  • Loading branch information
vlasfama authored May 6, 2023
1 parent b27114e commit 4ea8fcd
Show file tree
Hide file tree
Showing 46 changed files with 504 additions and 695 deletions.
4 changes: 2 additions & 2 deletions cmd/daemon/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ func Init() func(c *cli.Cmd) {
// makeLocalGenesis makes genesis file for the local network.
func makeLocalGenesis(pub *bls.PublicKey) *genesis.Genesis {
// Treasury account
acc := account.NewAccount(crypto.TreasuryAddress, 0)
acc := account.NewAccount(0)
acc.AddToBalance(21 * 1e14)
accs := []*account.Account{acc}
accs := map[crypto.Address]*account.Account{}

val := validator.NewValidator(pub, 0)
vals := []*validator.Validator{val}
Expand Down
5 changes: 3 additions & 2 deletions consensus/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,16 @@ func setup(t *testing.T) {
vals[i] = val
}

acc := account.NewAccount(crypto.TreasuryAddress, 0)
acc := account.NewAccount(0)
acc.AddToBalance(21 * 1e14)
accs := map[crypto.Address]*account.Account{crypto.TreasuryAddress: acc}
params := param.DefaultParams()
params.CommitteeSize = 4
params.BlockTimeInSecond = 1

// to prevent triggering timers before starting the tests to avoid double entries for new heights in some tests.
getTime := util.RoundNow(params.BlockTimeInSecond).Add(time.Duration(params.BlockTimeInSecond) * time.Second)
tGenDoc = genesis.MakeGenesis(getTime, []*account.Account{acc}, vals, params)
tGenDoc = genesis.MakeGenesis(getTime, accs, vals, params)
stX, err := state.LoadOrNewState(tGenDoc, []crypto.Signer{tSigners[tIndexX]}, store.MockingStore(), tTxPool, nil)
require.NoError(t, err)
stY, err := state.LoadOrNewState(tGenDoc, []crypto.Signer{tSigners[tIndexY]}, store.MockingStore(), tTxPool, nil)
Expand Down
5 changes: 3 additions & 2 deletions consensus/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

func TestManager(t *testing.T) {
_, committeeSigners := committee.GenerateTestCommittee(4)
acc := account.NewAccount(crypto.TreasuryAddress, 0)
acc := account.NewAccount(0)
acc.AddToBalance(21 * 1e14)
params := param.DefaultParams()
params.BlockTimeInSecond = 1
Expand All @@ -34,9 +34,10 @@ func TestManager(t *testing.T) {
val := validator.NewValidator(s.PublicKey().(*bls.PublicKey), int32(i))
vals[i] = val
}
accs := map[crypto.Address]*account.Account{crypto.TreasuryAddress: acc}
// to prevent triggering timers before starting the tests to avoid double entries for new heights in some tests.
getTime := util.RoundNow(params.BlockTimeInSecond).Add(time.Duration(params.BlockTimeInSecond) * time.Second)
genDoc := genesis.MakeGenesis(getTime, []*account.Account{acc}, vals, params)
genDoc := genesis.MakeGenesis(getTime, accs, vals, params)

rewardAddrs := []crypto.Address{
crypto.GenerateTestAddress(), crypto.GenerateTestAddress(),
Expand Down
29 changes: 13 additions & 16 deletions execution/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestExecution(t *testing.T) {
addr1 := signer1.Address()
acc1 := sb.MakeNewAccount(addr1)
acc1.AddToBalance(100 * 1e9)
sb.UpdateAccount(acc1)
sb.UpdateAccount(addr1, acc1)

rcvAddr := crypto.GenerateTestAddress()
block1 := sb.TestStore.AddTestBlock(1)
Expand Down Expand Up @@ -114,14 +114,13 @@ func TestChecker(t *testing.T) {
block1000 := sb.TestStore.AddTestBlock(1000)

t.Run("In strict mode transaction should be rejected.", func(t *testing.T) {
pub, prv := bls.GenerateTestKeyPair()
signer := crypto.NewSigner(prv)
acc := sb.MakeNewAccount(pub.Address())
signer := bls.GenerateTestSigner()
acc := sb.MakeNewAccount(signer.Address())
acc.AddToBalance(10000)
sb.UpdateAccount(acc)
sb.UpdateAccount(signer.Address(), acc)
valPub := sb.TestCommitteeSigners[0].PublicKey()

trx := tx.NewBondTx(block1000.Stamp(), acc.Sequence()+1, acc.Address(),
trx := tx.NewBondTx(block1000.Stamp(), acc.Sequence()+1, signer.Address(),
valPub.Address(), nil, 1000, 1000, "")
signer.SignMsg(trx)
assert.Error(t, executor.Execute(trx, sb))
Expand Down Expand Up @@ -166,13 +165,12 @@ func TestLockTime(t *testing.T) {
})

t.Run("Should reject expired transactions", func(t *testing.T) {
pub, prv := bls.GenerateTestKeyPair()
signer := crypto.NewSigner(prv)
acc := sb.MakeNewAccount(pub.Address())
signer := bls.GenerateTestSigner()
acc := sb.MakeNewAccount(signer.Address())
acc.AddToBalance(10000)
sb.UpdateAccount(acc)
sb.UpdateAccount(signer.Address(), acc)
pld := &payload.SendPayload{
Sender: acc.Address(),
Sender: signer.Address(),
Receiver: crypto.GenerateTestAddress(),
Amount: 1234,
}
Expand All @@ -185,13 +183,12 @@ func TestLockTime(t *testing.T) {
})

t.Run("Not finalized transaction", func(t *testing.T) {
pub, prv := bls.GenerateTestKeyPair()
signer := crypto.NewSigner(prv)
acc := sb.MakeNewAccount(pub.Address())
signer := bls.GenerateTestSigner()
acc := sb.MakeNewAccount(signer.Address())
acc.AddToBalance(10000)
sb.UpdateAccount(acc)
sb.UpdateAccount(signer.Address(), acc)
pld := &payload.SendPayload{
Sender: acc.Address(),
Sender: signer.Address(),
Receiver: crypto.GenerateTestAddress(),
Amount: 1234,
}
Expand Down
2 changes: 1 addition & 1 deletion execution/executor/bond.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (e *BondExecutor) Execute(trx *tx.Tx, sb sandbox.Sandbox) error {
val.AddToStake(pld.Stake)
val.UpdateLastBondingHeight(sb.CurrentHeight())

sb.UpdateAccount(senderAcc)
sb.UpdateAccount(pld.Sender, senderAcc)
sb.UpdateValidator(val)

e.fee = trx.Fee()
Expand Down
40 changes: 20 additions & 20 deletions execution/executor/bond_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ func TestExecuteBondTx(t *testing.T) {
setup(t)
exe := NewBondExecutor(true)

sender := tSandbox.TestStore.RandomTestAcc()
senderBalance := sender.Balance()
senderAddr, senderAcc := tSandbox.TestStore.RandomTestAcc()
senderBalance := senderAcc.Balance()
pub, _ := bls.GenerateTestKeyPair()
fee, amt := randomAmountAndFee(senderBalance / 2)

Expand All @@ -27,22 +27,22 @@ func TestExecuteBondTx(t *testing.T) {
})

t.Run("Should fail, invalid sequence", func(t *testing.T) {
trx := tx.NewBondTx(tStamp500000, sender.Sequence()+2, sender.Address(),
trx := tx.NewBondTx(tStamp500000, senderAcc.Sequence()+2, senderAddr,
pub.Address(), pub, amt, fee, "invalid sequence")
err := exe.Execute(trx, tSandbox)
assert.Equal(t, errors.Code(err), errors.ErrInvalidSequence)
})

t.Run("Should fail, insufficient balance", func(t *testing.T) {
trx := tx.NewBondTx(tStamp500000, sender.Sequence()+1, sender.Address(),
trx := tx.NewBondTx(tStamp500000, senderAcc.Sequence()+1, senderAddr,
pub.Address(), pub, senderBalance+1, 0, "insufficient balance")
err := exe.Execute(trx, tSandbox)
assert.Equal(t, errors.Code(err), errors.ErrInsufficientFunds)
})

t.Run("Should fail, inside committee", func(t *testing.T) {
pub := tSandbox.Committee().Proposer(0).PublicKey()
trx := tx.NewBondTx(tStamp500000, sender.Sequence()+1, sender.Address(),
trx := tx.NewBondTx(tStamp500000, senderAcc.Sequence()+1, senderAddr,
pub.Address(), nil, amt, fee, "inside committee")
err := exe.Execute(trx, tSandbox)
assert.Equal(t, errors.Code(err), errors.ErrInvalidTx)
Expand All @@ -54,22 +54,22 @@ func TestExecuteBondTx(t *testing.T) {
val.UpdateUnbondingHeight(tSandbox.CurrentHeight())
tSandbox.UpdateValidator(val)

trx := tx.NewBondTx(tStamp500000, sender.Sequence()+1, sender.Address(),
trx := tx.NewBondTx(tStamp500000, senderAcc.Sequence()+1, senderAddr,
pub.Address(), nil, amt, fee, "unbonded before")
err := exe.Execute(trx, tSandbox)
assert.Equal(t, errors.Code(err), errors.ErrInvalidHeight)
})

t.Run("Should fail, public key is not set", func(t *testing.T) {
trx := tx.NewBondTx(tStamp500000, sender.Sequence()+1, sender.Address(),
trx := tx.NewBondTx(tStamp500000, senderAcc.Sequence()+1, senderAddr,
pub.Address(), nil, amt, fee, "no public key")

err := exe.Execute(trx, tSandbox)
assert.Equal(t, errors.Code(err), errors.ErrInvalidPublicKey)
})

t.Run("Ok", func(t *testing.T) {
trx := tx.NewBondTx(tStamp500000, sender.Sequence()+1, sender.Address(),
trx := tx.NewBondTx(tStamp500000, senderAcc.Sequence()+1, senderAddr,
pub.Address(), pub, amt, fee, "ok")

assert.NoError(t, exe.Execute(trx, tSandbox))
Expand All @@ -79,14 +79,14 @@ func TestExecuteBondTx(t *testing.T) {
})

t.Run("Should fail, public key set for second bond", func(t *testing.T) {
trx := tx.NewBondTx(tStamp500000, sender.Sequence()+2, sender.Address(),
trx := tx.NewBondTx(tStamp500000, senderAcc.Sequence()+2, senderAddr,
pub.Address(), pub, amt, fee, "with public key")

err := exe.Execute(trx, tSandbox)
assert.Equal(t, errors.Code(err), errors.ErrInvalidPublicKey)
})

assert.Equal(t, tSandbox.Account(sender.Address()).Balance(),
assert.Equal(t, tSandbox.Account(senderAddr).Balance(),
senderBalance-(amt+fee))
assert.Equal(t, tSandbox.Validator(pub.Address()).Stake(), amt)
assert.Equal(t, tSandbox.Validator(pub.Address()).LastBondingHeight(),
Expand All @@ -104,12 +104,12 @@ func TestBondInsideCommittee(t *testing.T) {

exe1 := NewBondExecutor(true)
exe2 := NewBondExecutor(false)
sender := tSandbox.TestStore.RandomTestAcc()
senderBalance := sender.Balance()
senderAddr, senderAcc := tSandbox.TestStore.RandomTestAcc()
senderBalance := senderAcc.Balance()
fee, amt := randomAmountAndFee(senderBalance)

pub := tSandbox.Committee().Proposer(0).PublicKey()
trx := tx.NewBondTx(tStamp500000, sender.Sequence()+1, sender.Address(),
trx := tx.NewBondTx(tStamp500000, senderAcc.Sequence()+1, senderAddr,
pub.Address(), nil, amt, fee, "inside committee")

assert.Error(t, exe1.Execute(trx, tSandbox))
Expand All @@ -124,16 +124,16 @@ func TestBondJoiningCommittee(t *testing.T) {

exe1 := NewBondExecutor(true)
exe2 := NewBondExecutor(false)
sender := tSandbox.TestStore.RandomTestAcc()
senderBalance := sender.Balance()
senderAddr, senderAcc := tSandbox.TestStore.RandomTestAcc()
senderBalance := senderAcc.Balance()
pub, _ := bls.GenerateTestKeyPair()
fee, amt := randomAmountAndFee(senderBalance)

val := tSandbox.MakeNewValidator(pub)
val.UpdateLastJoinedHeight(tSandbox.CurrentHeight())
tSandbox.UpdateValidator(val)

trx := tx.NewBondTx(tStamp500000, sender.Sequence()+1, sender.Address(),
trx := tx.NewBondTx(tStamp500000, senderAcc.Sequence()+1, senderAddr,
pub.Address(), nil, amt, fee, "joining committee")

assert.Error(t, exe1.Execute(trx, tSandbox))
Expand All @@ -148,12 +148,12 @@ func TestStakeExceeded(t *testing.T) {
exe := NewBondExecutor(true)
amt := tSandbox.TestParams.MaximumStake + 1
fee := int64(float64(amt) * tSandbox.Params().FeeFraction)
sender := tSandbox.TestStore.RandomTestAcc()
sender.AddToBalance(tSandbox.TestParams.MaximumStake)
tSandbox.UpdateAccount(sender)
senderAddr, senderAcc := tSandbox.TestStore.RandomTestAcc()
senderAcc.AddToBalance(tSandbox.TestParams.MaximumStake)
tSandbox.UpdateAccount(senderAddr, senderAcc)
pub, _ := bls.GenerateTestKeyPair()

trx := tx.NewBondTx(tStamp500000, sender.Sequence()+1, sender.Address(),
trx := tx.NewBondTx(tStamp500000, senderAcc.Sequence()+1, senderAddr,
pub.Address(), pub, amt, fee, "stake exceeded")

assert.Error(t, exe.Execute(trx, tSandbox))
Expand Down
4 changes: 2 additions & 2 deletions execution/executor/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func (e *SendExecutor) Execute(trx *tx.Tx, sb sandbox.Sandbox) error {
senderAcc.SubtractFromBalance(pld.Amount + trx.Fee())
receiverAcc.AddToBalance(pld.Amount)

sb.UpdateAccount(senderAcc)
sb.UpdateAccount(receiverAcc)
sb.UpdateAccount(pld.Sender, senderAcc)
sb.UpdateAccount(pld.Receiver, receiverAcc)

e.fee = trx.Fee()

Expand Down
27 changes: 13 additions & 14 deletions execution/executor/send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,36 +46,36 @@ func TestExecuteSendTx(t *testing.T) {
setup(t)
exe := NewSendExecutor(true)

sender := tSandbox.TestStore.RandomTestAcc()
senderBalance := sender.Balance()
receiver := crypto.GenerateTestAddress()
senderAddr, senderAcc := tSandbox.TestStore.RandomTestAcc()
senderBalance := senderAcc.Balance()
receiverAddr := crypto.GenerateTestAddress()
amt, fee := randomAmountAndFee(senderBalance)

t.Run("Should fail, Sender has no account", func(t *testing.T) {
trx := tx.NewSendTx(tStamp500000, 1, crypto.GenerateTestAddress(), receiver, amt, fee, "non-existing account")
trx := tx.NewSendTx(tStamp500000, 1, crypto.GenerateTestAddress(), receiverAddr, amt, fee, "non-existing account")
assert.Equal(t, errors.Code(exe.Execute(trx, tSandbox)), errors.ErrInvalidAddress)
})

t.Run("Should fail, insufficient balance", func(t *testing.T) {
trx := tx.NewSendTx(tStamp500000, sender.Sequence()+1, sender.Address(), receiver, senderBalance+1, 0, "insufficient balance")
trx := tx.NewSendTx(tStamp500000, senderAcc.Sequence()+1, senderAddr, receiverAddr, senderBalance+1, 0, "insufficient balance")
assert.Equal(t, errors.Code(exe.Execute(trx, tSandbox)), errors.ErrInsufficientFunds)
})

t.Run("Should fail, Invalid sequence", func(t *testing.T) {
trx := tx.NewSendTx(tStamp500000, sender.Sequence()+2, sender.Address(), receiver, amt, fee, "invalid sequence")
trx := tx.NewSendTx(tStamp500000, senderAcc.Sequence()+2, senderAddr, receiverAddr, amt, fee, "invalid sequence")
assert.Equal(t, errors.Code(exe.Execute(trx, tSandbox)), errors.ErrInvalidSequence)
})

t.Run("Ok", func(t *testing.T) {
trx := tx.NewSendTx(tStamp500000, sender.Sequence()+1, sender.Address(), receiver, amt, fee, "ok")
trx := tx.NewSendTx(tStamp500000, senderAcc.Sequence()+1, senderAddr, receiverAddr, amt, fee, "ok")
assert.NoError(t, exe.Execute(trx, tSandbox))

// Execute again, should fail
assert.Error(t, exe.Execute(trx, tSandbox))
})

assert.Equal(t, tSandbox.Account(sender.Address()).Balance(), senderBalance-(amt+fee))
assert.Equal(t, tSandbox.Account(receiver).Balance(), amt)
assert.Equal(t, tSandbox.Account(senderAddr).Balance(), senderBalance-(amt+fee))
assert.Equal(t, tSandbox.Account(receiverAddr).Balance(), amt)

checkTotalCoin(t, fee)
}
Expand All @@ -84,15 +84,14 @@ func TestSendToSelf(t *testing.T) {
setup(t)
exe := NewSendExecutor(true)

sender := tSandbox.TestStore.RandomTestAcc()
senderBalance := sender.Balance()
senderAddr, senderAcc := tSandbox.TestStore.RandomTestAcc()
senderBalance := senderAcc.Balance()
amt, fee := randomAmountAndFee(senderBalance)

self := sender.Address()
trx := tx.NewSendTx(tStamp500000, sender.Sequence()+1, sender.Address(), sender.Address(), amt, fee, "ok")
trx := tx.NewSendTx(tStamp500000, senderAcc.Sequence()+1, senderAddr, senderAddr, amt, fee, "ok")
assert.NoError(t, exe.Execute(trx, tSandbox))

assert.Equal(t, tSandbox.Account(self).Balance(), senderBalance-fee) // Fee should be deducted
assert.Equal(t, tSandbox.Account(senderAddr).Balance(), senderBalance-fee) // Fee should be deducted
assert.Equal(t, exe.Fee(), fee)
}

Expand Down
Loading

0 comments on commit 4ea8fcd

Please sign in to comment.