Skip to content

Commit

Permalink
Merge branch 'goldworm-add-unit-tests-on-icsim'
Browse files Browse the repository at this point in the history
  • Loading branch information
goldworm committed Dec 4, 2023
2 parents 0411e1f + d9ad11a commit 8037353
Show file tree
Hide file tree
Showing 13 changed files with 787 additions and 309 deletions.
9 changes: 5 additions & 4 deletions icon/icsim/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/icon-project/goloop/service/scoreresult"
"github.com/icon-project/goloop/service/state"
"github.com/icon-project/goloop/service/trace"
"github.com/icon-project/goloop/service/txresult"
)

var (
Expand Down Expand Up @@ -242,8 +243,8 @@ func NewWorldContext(

type callContext struct {
WorldContext
from module.Address
events []*Event
from module.Address
events []*txresult.TestEventLog
}

func (ctx *callContext) From() module.Address {
Expand Down Expand Up @@ -291,11 +292,11 @@ func (ctx *callContext) SumOfStepUsed() *big.Int {
}

func (ctx *callContext) OnEvent(addr module.Address, indexed, data [][]byte) {
e := NewEvent(addr, indexed, data)
e := &txresult.TestEventLog{Address: addr, Indexed: indexed, Data: data}
ctx.events = append(ctx.events, e)
}

func (ctx *callContext) Events() []*Event {
func (ctx *callContext) Events() []*txresult.TestEventLog {
return ctx.events
}

Expand Down
70 changes: 35 additions & 35 deletions icon/icsim/doublesign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestDoubleSign_RequestUnjailNormalCase(t *testing.T) {
var err error
var dsBlockHeight int64
var csi module.ConsensusInfo
var rcpt Receipt
var receipts []Receipt

cfg := NewSimConfigWithParams(map[SimConfigOption]interface{}{
SCOTermPeriod: termPeriod,
Expand All @@ -57,54 +57,54 @@ func TestDoubleSign_RequestUnjailNormalCase(t *testing.T) {

prep0 := env.preps[0]
prep0Sub := env.preps[cfg.TotalMainPRepCount()]
prep := sim.GetPRep(prep0)
prep := sim.GetPRepByOwner(prep0)
assert.Equal(t, icstate.GradeMain, prep.Grade())
assert.Zero(t, prep.JailFlags())

// T(1) : SuccessCase(HandleDoubleSignReport)
dsType := module.DSTProposal
dsBlockHeight = sim.BlockHeight() - 10
rcpt, err = sim.GoByHandleDoubleSignReport(csi, state.SystemAddress, dsType, dsBlockHeight, prep0)
receipts, err = sim.GoByHandleDoubleSignReport(csi, state.SystemAddress, dsType, dsBlockHeight, prep0)
assert.NoError(t, err)
assert.True(t, CheckReceiptSuccess(rcpt))
assert.True(t, CheckReceiptSuccess(receipts[1]))
// Check the status of prep0
prep = sim.GetPRep(prep0)
prep = sim.GetPRepByOwner(prep0)
assert.Equal(t, icstate.GradeCandidate, prep.Grade())
assert.True(t, icutils.ContainsAll(prep.JailFlags(), icstate.JFlagInJail|icstate.JFlagDoubleSign))
assert.Zero(t, prep.MinDoubleSignHeight())
// Check the status of prep0Sub(prep25)
prep = sim.GetPRep(prep0Sub)
prep = sim.GetPRepByOwner(prep0Sub)
assert.Equal(t, icstate.GradeMain, prep.Grade())

// Move to the block which is 5 blocks earlier
term = sim.TermSnapshot()
assert.NoError(t, sim.GoTo(csi, term.GetEndHeight()-5))

// T(100 - 5) : DoubleSignReport for the PRep with JailFlagDoubleSign is ignored silently (Success)
rcpt, err = sim.GoByHandleDoubleSignReport(csi, state.SystemAddress, module.DSTVote, dsBlockHeight+1, prep0)
receipts, err = sim.GoByHandleDoubleSignReport(csi, state.SystemAddress, module.DSTVote, dsBlockHeight+1, prep0)
assert.NoError(t, err)
assert.True(t, rcpt.Status() == 1)
assert.True(t, receipts[1].Status() == 1)

// T(100 - 5 + 1)
assert.NoError(t, sim.GoToTermEnd(csi))

// Next Term

// T(0) : RequestUnjail transaction
prep = sim.GetPRep(prep0)
prep = sim.GetPRepByOwner(prep0)
assert.Equal(t, icstate.GradeCandidate, prep.Grade())
rcpt, err = sim.GoByRequestUnjail(csi, prep0)
receipts, err = sim.GoByRequestUnjail(csi, prep0)
assert.NoError(t, err)
assert.True(t, CheckReceiptSuccess(rcpt))
prep = sim.GetPRep(prep0)
assert.True(t, CheckReceiptSuccess(receipts[1]))
prep = sim.GetPRepByOwner(prep0)
assert.True(t, icutils.ContainsAll(
prep.JailFlags(), icstate.JFlagUnjailing|icstate.JFlagDoubleSign|icstate.JFlagInJail))
assert.Equal(t, icstate.GradeCandidate, prep.Grade())
assert.True(t, prep.IsJailInfoElectable())

// T(2) : Go to term end
assert.NoError(t, sim.GoToTermEnd(nil))
prep = sim.GetPRep(prep0)
prep = sim.GetPRepByOwner(prep0)
assert.Equal(t, icstate.GradeMain, prep.Grade())
assert.Zero(t, prep.JailFlags())
assert.Equal(t, sim.BlockHeight(), prep.MinDoubleSignHeight())
Expand All @@ -119,7 +119,7 @@ func TestHandleDoubleSignReport_Slashing(t *testing.T) {
var err error
var dsBlockHeight int64
var csi module.ConsensusInfo
var rcpt Receipt
var receipts []Receipt
var revision module.Revision
slashingRate := icmodule.ToRate(10)

Expand All @@ -138,17 +138,17 @@ func TestHandleDoubleSignReport_Slashing(t *testing.T) {
term := sim.TermSnapshot()
assert.Equal(t, icstate.IISSVersion3, term.GetIISSVersion())
revision = icmodule.ValueToRevision(icmodule.RevisionIISS4R1)
rcpt, err = sim.GoBySetRevision(csi, env.Governance(), revision)
receipts, err = sim.GoBySetRevision(csi, env.Governance(), revision)
assert.NoError(t, err)
assert.True(t, CheckReceiptSuccess(rcpt))
assert.True(t, CheckReceiptSuccess(receipts[1]))

// T(1)
const penaltyType = icmodule.PenaltyDoubleSign
rcpt, err = sim.GoBySetSlashingRates(csi, env.Governance(), map[string]icmodule.Rate{
receipts, err = sim.GoBySetSlashingRates(csi, env.Governance(), map[string]icmodule.Rate{
penaltyType.String(): slashingRate,
})
assert.NoError(t, err)
assert.True(t, CheckReceiptSuccess(rcpt))
assert.True(t, CheckReceiptSuccess(receipts[1]))
jso, err := sim.GetSlashingRates()
assert.NoError(t, err)
assert.Equal(t, slashingRate.NumInt64(), jso[penaltyType.String()])
Expand All @@ -162,31 +162,31 @@ func TestHandleDoubleSignReport_Slashing(t *testing.T) {

prep0 := env.preps[0]
prep0Sub := env.preps[cfg.TotalMainPRepCount()]
prep := sim.GetPRep(prep0)
prep := sim.GetPRepByOwner(prep0)
assert.Equal(t, icstate.GradeMain, prep.Grade())
assert.Zero(t, prep.JailFlags())

// T(1) : SuccessCase(HandleDoubleSignReport)
prep = sim.GetPRep(prep0)
prep = sim.GetPRepByOwner(prep0)
oldBonded := prep.Bonded()
oldTotalSupply := sim.TotalSupply()
oldTotalStake := sim.TotalStake()

dsType := module.DSTProposal
dsBlockHeight = sim.BlockHeight() - 10
rcpt, err = sim.GoByHandleDoubleSignReport(csi, state.SystemAddress, dsType, dsBlockHeight, prep0)
receipts, err = sim.GoByHandleDoubleSignReport(csi, state.SystemAddress, dsType, dsBlockHeight, prep0)
assert.NoError(t, err)
assert.True(t, CheckReceiptSuccess(rcpt))
assert.True(t, CheckReceiptSuccess(receipts[1]))
// Check the status of prep0
prep = sim.GetPRep(prep0)
prep = sim.GetPRepByOwner(prep0)
assert.Equal(t, icstate.GradeCandidate, prep.Grade())
assert.True(t, icutils.ContainsAll(prep.JailFlags(), icstate.JFlagInJail|icstate.JFlagDoubleSign))
assert.Zero(t, prep.MinDoubleSignHeight())
// Check the status of prep0Sub(prep25)
prep = sim.GetPRep(prep0Sub)
prep = sim.GetPRepByOwner(prep0Sub)
assert.Equal(t, icstate.GradeMain, prep.Grade())
// Slashing for DoubleSignPenalty
prep = sim.GetPRep(prep0)
prep = sim.GetPRepByOwner(prep0)
newBonded := prep.Bonded()
slashed := estimateSlashed(slashingRate, oldBonded)
assert.Zero(t, newBonded.Cmp(new(big.Int).Sub(oldBonded, slashed)))
Expand All @@ -203,7 +203,7 @@ func TestDoubleSign_HandleDoubleSignReportErrorCases(t *testing.T) {
)
var err error
var csi module.ConsensusInfo
var rcpt Receipt
var receipts []Receipt

cfg := NewSimConfigWithParams(map[SimConfigOption]interface{}{
SCOTermPeriod: termPeriod,
Expand Down Expand Up @@ -249,13 +249,13 @@ func TestDoubleSign_HandleDoubleSignReportErrorCases(t *testing.T) {
}

// ErrorCase(InvalidDoubleSignBlockHeight)
rcpt, err = sim.GoByHandleDoubleSignReport(
receipts, err = sim.GoByHandleDoubleSignReport(
csi, state.SystemAddress, arg.dsType, dsBlockHeight, arg.signer)
assert.NoError(t, err)
assert.True(t, rcpt.Status() == 0)
assert.True(t, receipts[1].Status() == 0)

// No impact on PRep's JailFlags
prep := sim.GetPRep(prep0)
prep := sim.GetPRepByOwner(prep0)
assert.Zero(t, prep.JailFlags())
assert.Equal(t, icstate.GradeMain, prep.Grade())
assert.Zero(t, prep.MinDoubleSignHeight())
Expand All @@ -269,7 +269,7 @@ func TestDoubleSign_RequestUnjailForNormalPRep(t *testing.T) {
)
var err error
var csi module.ConsensusInfo
var rcpt Receipt
var receipts []Receipt

cfg := NewSimConfigWithParams(map[SimConfigOption]interface{}{
SCOTermPeriod: termPeriod,
Expand All @@ -289,13 +289,13 @@ func TestDoubleSign_RequestUnjailForNormalPRep(t *testing.T) {

// T(0)
prep0 := env.preps[0]
prep := sim.GetPRep(prep0)
prep := sim.GetPRepByOwner(prep0)
assert.Equal(t, icstate.GradeMain, prep.Grade())
assert.Zero(t, prep.JailFlags())

// UnjailRequest for a normal PRep will cause transaction failure
rcpt, err = sim.GoByRequestUnjail(csi, prep0)
receipts, err = sim.GoByRequestUnjail(csi, prep0)
assert.NoError(t, err)
assert.Zero(t, rcpt.Status())
assert.Zero(t, sim.GetPRep(prep0).JailFlags())
}
assert.Zero(t, receipts[1].Status())
assert.Zero(t, sim.GetPRepByOwner(prep0).JailFlags())
}
8 changes: 4 additions & 4 deletions icon/icsim/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,11 @@ func (env *Env) defaultInitOnRev(revision int) error {
var csi module.ConsensusInfo

sim := env.sim
rcpt, err := sim.GoBySetRevision(csi, env.Governance(), icmodule.ValueToRevision(revision))
receipts, err := sim.GoBySetRevision(csi, env.Governance(), icmodule.ValueToRevision(revision))
if err != nil {
return err
}
if !CheckReceiptSuccess(rcpt) {
if !CheckReceiptSuccess(receipts[1]) {
return errors.Errorf("SetRevisionFailure(%d)", revision)
}

Expand All @@ -284,11 +284,11 @@ func (env *Env) initOnRev13(revision int) error {
var receipts []Receipt
sim := env.Simulator()

rcpt, err := sim.GoBySetRevision(csi, env.Governance(), icmodule.ValueToRevision(revision))
receipts, err = sim.GoBySetRevision(csi, env.Governance(), icmodule.ValueToRevision(revision))
if err != nil {
return err
}
if !CheckReceiptSuccess(rcpt) {
if !CheckReceiptSuccess(receipts[1]) {
return errors.Errorf("SetRevisionFailure(%d)", revision)
}

Expand Down
22 changes: 11 additions & 11 deletions icon/icsim/extramainprep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ func Test_ExtraMainPReps(t *testing.T) {
env, err := NewEnv(c, icmodule.Revision13)
assert.NoError(t, err)
sim := env.sim
csi = sim.NewDefaultConsensusInfo()
csi = NewConsensusInfoBySim(sim)

// Set revision to 17 to activate extra main preps
rcpt, err := sim.GoBySetRevision(csi, env.Governance(), icmodule.RevisionExtraMainPReps)
receipts, err := sim.GoBySetRevision(csi, env.Governance(), icmodule.RevisionExtraMainPReps)
assert.NoError(t, err)
assert.True(t, CheckReceiptSuccess(rcpt))
assert.True(t, CheckReceiptSuccess(receipts[1]))
err = sim.GoToTermEnd(csi)
assert.NoError(t, err)

vl = sim.ValidatorList()
assert.Len(t, vl, newMainPRepCount)
csi = sim.NewDefaultConsensusInfo()
csi = NewConsensusInfoBySim(sim)

bonders := make(map[string]bool)

Expand All @@ -62,9 +62,9 @@ func Test_ExtraMainPReps(t *testing.T) {
}
for i := 0; i < size; i++ {
bonder := env.bonders[i]
rcpt, err = sim.GoBySetBond(csi, bonder, emptyBonds)
receipts, err = sim.GoBySetBond(csi, bonder, emptyBonds)
assert.NoError(t, err)
assert.True(t, CheckReceiptSuccess(rcpt))
assert.True(t, CheckReceiptSuccess(receipts[1]))

assert.False(t, bonders[icutils.ToKey(bonder)])
bonders[icutils.ToKey(bonder)] = true
Expand All @@ -78,9 +78,9 @@ func Test_ExtraMainPReps(t *testing.T) {
bonderList := sim.GetBonderList(owner)

assert.False(t, bonders[icutils.ToKey(bonderList[0])])
rcpt, err = sim.GoBySetBond(csi, bonderList[0], emptyBonds)
receipts, err = sim.GoBySetBond(csi, bonderList[0], emptyBonds)
assert.NoError(t, err)
assert.True(t, CheckReceiptSuccess(rcpt))
assert.True(t, CheckReceiptSuccess(receipts[1]))

// Move to the next term
assert.NoError(t, sim.GoToTermEnd(csi))
Expand All @@ -89,18 +89,18 @@ func Test_ExtraMainPReps(t *testing.T) {
vl = sim.ValidatorList()
assert.Len(t, vl, newMainPRepCount-i-1)
for _, v := range vl {
prep := sim.GetPRep(v.Address())
prep := sim.GetPRepByOwner(v.Address())
assert.True(t, prep.Bonded().Sign() > 0)
assert.True(t, prep.GetPower(br).Sign() > 0)
}

bondedPReps := 0
for _, address := range env.preps {
prep := sim.GetPRep(address)
prep := sim.GetPRepByOwner(address)
if prep.Bonded().Sign() > 0 {
bondedPReps++
}
}
assert.Equal(t, newMainPRepCount-i-1, bondedPReps)
}
}
}
Loading

0 comments on commit 8037353

Please sign in to comment.