Best Go-testdeep code snippet using td.Summary
rewards_penalties.go
Source:rewards_penalties.go
...39 for _, bob := range []addr.Address{bobPk, bobId} {40 aBal := td.GetBalance(aliceId)41 bBal := td.GetBalance(bobId)42 burnBal := td.GetBalance(builtin.BurntFundsActorAddr)43 prevRewards := td.GetRewardSummary()44 prevMinerBal := td.GetBalance(miner)45 // Process a block with two messages, a simple send back and forth between accounts.46 msg1 := td.MessageProducer.Transfer(alice, bob, chain.Value(sendValue), chain.Nonce(callSeq))47 msg2 := td.MessageProducer.Transfer(bob, alice, chain.Value(sendValue), chain.Nonce(callSeq))48 result := tipB.WithBlockBuilder(49 drivers.NewBlockBuilder(td, td.ExeCtx.Miner).50 WithBLSMessageOk(msg1).51 WithBLSMessageOk(msg2),52 ).ApplyAndValidate()53 tipB.Clear()54 td.ExeCtx.Epoch++55 // Each account has paid gas fees.56 td.AssertBalance(aliceId, big.Sub(aBal, td.CalcMessageCost(msg1.GasLimit, msg1.GasPremium, big.Zero(), result.Receipts[0])))57 td.AssertBalance(bobId, big.Sub(bBal, td.CalcMessageCost(msg2.GasLimit, msg2.GasPremium, big.Zero(), result.Receipts[1])))58 gasSum := msg1.GasLimit + msg2.GasLimit // Exploit gas premium = 159 // Validate rewards are paid directly to miner60 newRewards := td.GetRewardSummary()61 // total supply should decrease by the last reward amount62 assert.Equal(t, big.Sub(prevRewards.Treasury, prevRewards.NextPerBlockReward), newRewards.Treasury)63 // the miners balance should have increased by the reward amount64 thisReward := big.Add(prevRewards.NextPerBlockReward, big.NewInt(gasSum))65 assert.Equal(t, big.Add(prevMinerBal, thisReward), td.GetBalance(miner))66 newBurn := big.Add(drivers.GetBurn(types.GasUnits(msg1.GasLimit), result.Receipts[0].GasUsed), drivers.GetBurn(types.GasUnits(msg2.GasLimit), result.Receipts[1].GasUsed))67 td.AssertBalance(builtin.BurntFundsActorAddr, big.Add(burnBal, newBurn))68 callSeq++69 }70 }71 })72 t.Run("penalize sender doesn't exist", func(t *testing.T) {73 td := builder.Build(t)74 defer td.Complete()75 bb := drivers.NewBlockBuilder(td, td.ExeCtx.Miner)76 miner := td.ExeCtx.Miner77 _, receiver := td.NewAccountActor(drivers.SECP, acctDefaultBalance)78 badSenders := []addr.Address{79 utils.NewIDAddr(t, 1234),80 utils.NewSECP256K1Addr(t, "1234"),81 utils.NewBLSAddr(t, 1234),82 utils.NewActorAddr(t, "1234"),83 }84 for _, s := range badSenders {85 bb.WithBLSMessageAndCode(td.MessageProducer.Transfer(s, receiver, chain.Value(sendValue)),86 exitcode.SysErrSenderInvalid,87 )88 }89 prevRewards := td.GetRewardSummary()90 prevMinerBalance := td.GetBalance(miner)91 drivers.NewTipSetMessageBuilder(td).WithBlockBuilder(bb).ApplyAndValidate()92 // Nothing received, no actors created.93 td.AssertBalance(receiver, acctDefaultBalance)94 for _, s := range badSenders {95 td.AssertNoActor(s)96 }97 newRewards := td.GetRewardSummary()98 newMinerBalance := td.GetBalance(miner)99 gasPenalty := drivers.GetMinerPenalty(gasLimit)100 gasPenalty = big.Mul(gasPenalty, big.NewInt(int64(len(badSenders))))101 // The penalty amount has been burnt by the reward actor, and subtracted from the miner's block reward102 validateRewards(td, prevRewards, newRewards, prevMinerBalance, newMinerBalance, big.Zero(), gasPenalty)103 td.AssertBalance(builtin.BurntFundsActorAddr, gasPenalty)104 })105 t.Run("penalize sender non account", func(t *testing.T) {106 td := builder.Build(t)107 defer td.Complete()108 miner := td.ExeCtx.Miner109 tb := drivers.NewTipSetMessageBuilder(td)110 bb := drivers.NewBlockBuilder(td, miner)111 _, receiver := td.NewAccountActor(drivers.SECP, acctDefaultBalance)112 // Various non-account actors that can't be top-level senders.113 senders := []addr.Address{114 builtin.SystemActorAddr,115 builtin.InitActorAddr,116 builtin.CronActorAddr,117 miner,118 }119 for _, sender := range senders {120 bb.WithBLSMessageAndCode(td.MessageProducer.Transfer(sender, receiver, chain.Value(sendValue)),121 exitcode.SysErrSenderInvalid)122 }123 prevRewards := td.GetRewardSummary()124 prevMinerBalance := td.GetBalance(miner)125 tb.WithBlockBuilder(bb).ApplyAndValidate()126 td.AssertBalance(receiver, acctDefaultBalance)127 newRewards := td.GetRewardSummary()128 newMinerBalance := td.GetBalance(miner)129 gasPenalty := drivers.GetMinerPenalty(gasLimit)130 gasPenalty = big.Mul(gasPenalty, big.NewInt(int64(len(senders))))131 // The penalty amount has been burnt by the reward actor, and subtracted from the miner's block reward.132 validateRewards(td, prevRewards, newRewards, prevMinerBalance, newMinerBalance, big.Zero(), gasPenalty)133 td.AssertBalance(builtin.BurntFundsActorAddr, gasPenalty)134 })135 t.Run("penalize wrong callseqnum", func(t *testing.T) {136 td := builder.Build(t)137 defer td.Complete()138 miner := td.ExeCtx.Miner139 tb := drivers.NewTipSetMessageBuilder(td)140 bb := drivers.NewBlockBuilder(td, td.ExeCtx.Miner)141 _, aliceId := td.NewAccountActor(drivers.BLS, acctDefaultBalance)142 bb.WithBLSMessageAndCode(143 td.MessageProducer.Transfer(aliceId, builtin.BurntFundsActorAddr, chain.Nonce(1)),144 exitcode.SysErrSenderStateInvalid,145 )146 prevRewards := td.GetRewardSummary()147 prevMinerBalance := td.GetBalance(miner)148 tb.WithBlockBuilder(bb).ApplyAndValidate()149 newRewards := td.GetRewardSummary()150 newMinerBalance := td.GetBalance(miner)151 gasPenalty := drivers.GetMinerPenalty(gasLimit)152 validateRewards(td, prevRewards, newRewards, prevMinerBalance, newMinerBalance, big.Zero(), gasPenalty)153 td.AssertBalance(builtin.BurntFundsActorAddr, gasPenalty)154 })155 t.Run("penalty if the balance is not sufficient to cover gas", func(t *testing.T) {156 td := builder.Build(t)157 defer td.Complete()158 miner := td.ExeCtx.Miner159 tb := drivers.NewTipSetMessageBuilder(td)160 bb := drivers.NewBlockBuilder(td, td.ExeCtx.Miner)161 balance := abi.NewTokenAmount(1)162 _, aliceId := td.NewAccountActor(drivers.BLS, balance)163 bb.WithBLSMessageAndCode(164 td.MessageProducer.Transfer(aliceId, builtin.BurntFundsActorAddr, chain.Value(big.Zero()), chain.Nonce(0), chain.GasLimit(gasLimit)),165 exitcode.SysErrSenderStateInvalid,166 )167 prevRewards := td.GetRewardSummary()168 prevMinerBalance := td.GetBalance(miner)169 tb.WithBlockBuilder(bb).ApplyAndValidate()170 newRewards := td.GetRewardSummary()171 newMinerBalance := td.GetBalance(miner)172 // The penalty charged to the miner is not present in the receipt so we just have to hardcode it here.173 validateRewards(td, prevRewards, newRewards, prevMinerBalance, newMinerBalance, big.Zero(), drivers.GetMinerPenalty(gasLimit))174 td.AssertBalance(aliceId, balance)175 })176 t.Run("no penalty if the balance is not sufficient to cover transfer", func(t *testing.T) {177 td := builder.Build(t)178 defer td.Complete()179 miner := td.ExeCtx.Miner180 tb := drivers.NewTipSetMessageBuilder(td)181 bb := drivers.NewBlockBuilder(td, td.ExeCtx.Miner)182 halfBalance := abi.NewTokenAmount(5_000_000_000_000)183 _, aliceId := td.NewAccountActor(drivers.BLS, big.Add(halfBalance, halfBalance))184 // Attempt to whole balance, in two parts.185 // The second message should fail (insufficient balance to pay fees).186 msgOk := td.MessageProducer.Transfer(aliceId, builtin.BurntFundsActorAddr, chain.Value(halfBalance))187 msgFail := td.MessageProducer.Transfer(aliceId, builtin.BurntFundsActorAddr, chain.Value(halfBalance), chain.Nonce(1))188 bb.WithBLSMessageOk(msgOk).WithBLSMessageAndCode(msgFail, exitcode.SysErrInsufficientFunds)189 prevRewards := td.GetRewardSummary()190 prevMinerBalance := td.GetBalance(miner)191 result := tb.WithBlockBuilder(bb).ApplyAndValidate()192 newRewards := td.GetRewardSummary()193 newMinerBalance := td.GetBalance(miner)194 // The penalty charged to the miner is not present in the receipt so we just have to hardcode it here.195 gasPenalty := big.NewInt(0)196 validateRewards(td, prevRewards, newRewards, prevMinerBalance, newMinerBalance, big.NewInt(msgOk.GasLimit+msgFail.GasLimit), gasPenalty)197 burn := big.Add(drivers.GetBurn(types.GasUnits(msgOk.GasLimit), result.Receipts[0].GasUsed), drivers.GetBurn(types.GasUnits(msgFail.GasLimit), result.Receipts[1].GasUsed))198 td.AssertBalance(builtin.BurntFundsActorAddr, big.Add(burn, big.Add(halfBalance, gasPenalty)))199 })200 t.Run("insufficient gas to cover return value", func(t *testing.T) {201 td := builder.Build(t)202 defer td.Complete()203 miner := td.ExeCtx.Miner204 tb := drivers.NewTipSetMessageBuilder(td)205 alice, _ := td.NewAccountActor(drivers.BLS, acctDefaultBalance)206 // get a successful result so we can determine how much gas it costs. We'll reduce this by 1 in a subsequent call207 // to test insufficient gas to cover return value.208 tracerResult := tb.WithBlockBuilder(209 drivers.NewBlockBuilder(td, td.ExeCtx.Miner).210 WithBLSMessageAndRet(td.MessageProducer.MinerControlAddresses(alice, miner, nil, chain.Nonce(0)),211 // required to satisfy testing methods, unrelated to current test.212 chain.MustSerialize(&miner_spec.GetControlAddressesReturn{213 Owner: td.StateDriver.BuiltinMinerInfo().OwnerID,214 Worker: td.StateDriver.BuiltinMinerInfo().WorkerID,215 }),216 ),217 ).ApplyAndValidate()218 requiredGasLimit := tracerResult.Receipts[0].GasUsed219 /* now the test */220 tb.Clear()221 rewardsBefore := td.GetRewardSummary()222 minerBalanceBefore := td.GetBalance(miner)223 senderBalanceBefore := td.GetBalance(alice)224 td.ExeCtx.Epoch++225 // Apply the message again with a reduced gas limit226 // A value just one less than the required limit for success ensures that the gas limit will be reached227 // at the last possible gas charge, which is that for the return value size.228 gasLimit := requiredGasLimit - 1229 result := tb.WithBlockBuilder(230 drivers.NewBlockBuilder(td, td.ExeCtx.Miner).231 WithBLSMessageAndCode(td.MessageProducer.MinerControlAddresses(alice, miner, nil, chain.Nonce(1), chain.GasLimit(int64(gasLimit))),232 exitcode.SysErrOutOfGas,233 ),234 ).ApplyAndValidate()235 gasUsed := result.Receipts[0].GasUsed236 gasCost := gasLimit.Big() // Gas price is 1237 newRewards := td.GetRewardSummary()238 // Check the actual gas charged is equal to the gas limit rather than the amount consumed up to but excluding239 // the return value which is smaller than the gas limit.240 assert.Equal(t, gasLimit, gasUsed)241 cost := td.CalcMessageCost(gasLimit.Big().Int64(), big.NewInt(gasPremium), big.Zero(), result.Receipts[0])242 // Check sender charged exactly the max cost.243 assert.Equal(td.T, big.Sub(senderBalanceBefore, cost), td.GetBalance(alice))244 // Check the miner earned exactly the max cost (plus block reward).245 thisRwd := big.Add(rewardsBefore.NextPerBlockReward, gasCost)246 assert.Equal(td.T, big.Add(minerBalanceBefore, thisRwd), td.GetBalance(miner))247 assert.Equal(td.T, big.Sub(rewardsBefore.Treasury, rewardsBefore.NextPerBlockReward), newRewards.Treasury)248 })249 // TODO more tests:250 // - miner penalty causes subsequent otherwise-valid message to have wrong nonce (another miner penalty)251 // - miner penalty followed by non-miner penalty with same nonce (in different block)252}253func validateRewards(td *drivers.TestDriver, prevRewards *drivers.RewardSummary, newRewards *drivers.RewardSummary, oldMinerBalance abi.TokenAmount, newMinerBalance abi.TokenAmount, gasReward big.Int, gasPenalty big.Int) {254 rwd := big.Add(big.Sub(prevRewards.NextPerBlockReward, gasPenalty), gasReward)255 assert.Equal(td.T, big.Add(oldMinerBalance, rwd), newMinerBalance)256 assert.Equal(td.T, big.Sub(prevRewards.Treasury, prevRewards.NextPerBlockReward), newRewards.Treasury)257}...
tdigest_test.go
Source:tdigest_test.go
...133 col3 := col()134 col3.MergeC(col2)135 assert.Equal(t, len(col3.Get().Results), 1)136}137func TestTDigestSummaryCollector(t *testing.T) {138 co1 := NewTDigestCollectorBuilder("summary")139 co2 := NewTDigestCollectorBuilder("")140 col1 := co1()141 col2 := co2()142 assert.Equal(t, col1.Type(), "summary")143 assert.Equal(t, col2.Type(), "summary")144 res1 := &fcom.Result{}145 res2 := &fcom.Result{146 BuildTime: time.Now().UnixNano(),147 }148 col1.Add(res1)149 assert.Equal(t, col1.(*TDigestSummaryCollector).Data.Num, 0)150 col2.Add(res2)151 assert.Equal(t, col2.(*TDigestSummaryCollector).Data.Num, 1)152 var bs []byte153 col2.Merge(bs)154 assert.Equal(t, col2.(*TDigestSummaryCollector).Data.Num, 1)155 col1.Add(res2)156 bs = col1.Serialize()157 assert.NotNil(t, bs)158 col2.Merge(bs)159 assert.Equal(t, col2.(*TDigestSummaryCollector).Data.Num, 2)160 col1.MergeC(col2)161 assert.Equal(t, col1.(*TDigestSummaryCollector).Data.Num, 3)162 col1.Reset()163 assert.Equal(t, col1.(*TDigestSummaryCollector).Data.Num, 0)164 assert.Equal(t, len(col1.Get().Results), 1)165}...
Summary
Using AI Code Generation
1import "fmt"2type td struct {3}4func (t td) Summary() int {5}6func main() {7 fmt.Println(t.Summary())8}9import "fmt"10type td struct {11}12func (t td) Summary() int {13}14func main() {15 fmt.Println(t.Summary())16}17import "fmt"18type td struct {19}20func (t td) Summary() int {21}22func main() {23 fmt.Println(t.Summary())24}25import "fmt"26type td struct {27}28func (t td) Summary() int {29}30func main() {31 fmt.Println(t.Summary())32}33import "fmt"34type td struct {35}36func (t td) Summary() int {37}38func main() {39 fmt.Println(t.Summary())40}41import "fmt"42type td struct {43}44func (t td) Summary() int {45}46func main() {47 fmt.Println(t.Summary())48}
Summary
Using AI Code Generation
1import "fmt"2func main() {3 t1 := td{1, 2}4 t2 := td{3, 4}5 t3 := td{5, 6}6 t4 := td{7, 8}7 t5 := td{9, 10}8 t6 := td{11, 12}9 t7 := td{13, 14}10 t8 := td{15, 16}11 t9 := td{17, 18}12 t10 := td{19, 20}13 t11 := td{21, 22}14 t12 := td{23, 24}15 t13 := td{25, 26}16 t14 := td{27, 28}17 t15 := td{29, 30}18 t16 := td{31, 32}19 t17 := td{33, 34}20 t18 := td{35, 36}21 t19 := td{37, 38}22 t20 := td{39, 40}23 t21 := td{41, 42}24 t22 := td{43, 44}25 t23 := td{45, 46}26 t24 := td{47, 48}27 t25 := td{49, 50}28 t26 := td{51, 52}29 t27 := td{53, 54}30 t28 := td{55, 56}31 t29 := td{57, 58}32 t30 := td{59, 60}33 t31 := td{61, 62}34 t32 := td{63, 64}35 t33 := td{65, 66}36 t34 := td{67, 68}37 t35 := td{69, 70}38 t36 := td{71, 72}39 t37 := td{73, 74}40 t38 := td{75, 76}41 t39 := td{77, 78}42 t40 := td{79, 80}43 t41 := td{81, 82}44 t42 := td{83, 84}45 t43 := td{85, 86}46 t44 := td{87, 88}
Summary
Using AI Code Generation
1import "fmt"2func main() {3 t := td{1, 2}4 fmt.Println(t.Summary())5}6import "fmt"7func main() {8 t := td{1, 2}9 fmt.Println(t.Summary())10}11import "fmt"12func main() {13 t := td{1, 2}14 fmt.Println(t.Summary())15}16import "fmt"17func main() {18 t := td{1, 2}19 fmt.Println(t.Summary())20}21import "fmt"22func main() {23 t := td{1, 2}24 fmt.Println(t.Summary())25}26import "fmt"27func main() {28 t := td{1, 2}29 fmt.Println(t.Summary())30}31import "fmt"32func main() {33 t := td{1, 2}34 fmt.Println(t.Summary())35}36import "fmt"37func main() {38 t := td{1, 2}39 fmt.Println(t.Summary())40}41import "fmt"42func main() {43 t := td{1, 2}44 fmt.Println(t.Summary())45}46import "fmt"47func main() {48 t := td{1, 2}49 fmt.Println(t.Summary())50}51import "fmt"52func main() {53 t := td{1, 2}54 fmt.Println(t.Summary())55}
Summary
Using AI Code Generation
1import "fmt"2func main() {3 td = td{10, 20}4 fmt.Println(td.Summary())5}6import "fmt"7func main() {8 td = td{10, 20}9 fmt.Println(td.Summary())10}11import "fmt"12func main() {13 td = td{10, 20}14 fmt.Println(td.Summary())15}16import "fmt"17func main() {18 td = td{10, 20}19 fmt.Println(td.Summary())20}21import "fmt"22func main() {23 td = td{10, 20}24 fmt.Println(td.Summary())25}26import "fmt"27func main() {28 td = td{10, 20}29 fmt.Println(td.Summary())30}31import "fmt"32func main() {33 td = td{10, 20}34 fmt.Println(td.Summary())35}36import "fmt"37func main() {38 td = td{10, 20}39 fmt.Println(td.Summary())40}41import "fmt"42func main() {43 td = td{10, 20}44 fmt.Println(td.Summary())45}46import "fmt"47func main() {48 td = td{10, 20}49 fmt.Println(td.Summary())50}
Summary
Using AI Code Generation
1import (2func main() {3 t := td.NewTimeDuration(1, 2, 3, 4)4 fmt.Println(t.Summary())5}6import (7type TimeDuration struct {8}9func (t TimeDuration) Summary() string {10 return fmt.Sprintf("%dh%dm%ds%dms", t.hours, t.minutes, t.seconds, t.milliseconds)11}12func main() {13 t := TimeDuration{1, 2, 3, 4}14 fmt.Println(t.Summary())15}
Summary
Using AI Code Generation
1import (2func main() {3 t := td.New(5, 10)4 fmt.Println(t.Summary())5}6import (7func main() {8 t := td.New(5, 10)9 fmt.Println(t.Summary())10}11import (12func main() {13 t := td.New(5, 10)14 fmt.Println(t.Summary())15}
Summary
Using AI Code Generation
1import (2func main() {3 t.Summary()4}5import (6func (t Td) Summary() {7 fmt.Println("Age:", t)8}9func main() {10 t.Summary()11}12import (13func (t Td) Summary() {14 fmt.Println("Age:", t)15}16func main() {17 t.Summary()18}
Summary
Using AI Code Generation
1import (2func main() {3 a.Summary()4 fmt.Println(a)5}6{0 0 0 0}7{0 0 0 0}
Summary
Using AI Code Generation
1import "fmt"2func main() {3 td := new(Td)4 td.Set(2, 3, 4, 5)5 fmt.Println(td.Summary())6}
Summary
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello World")4 a := test.TD{1, 2, 3}5 fmt.Println(a.Summary())6}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!