Skip to content

Commit

Permalink
fix: Fix burnlog not presented
Browse files Browse the repository at this point in the history
  • Loading branch information
howjmay committed Sep 15, 2023
1 parent ac78945 commit 0e70478
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 14 deletions.
6 changes: 6 additions & 0 deletions packages/vm/core/blocklog/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ func (rec *RequestReceipt) Read(r io.Reader) error {
rec.Error = new(isc.UnresolvedVMError)
rr.Read(rec.Error)
}
fmt.Println("rr.Read(rec.GasBurnLog: 1")
rr.Read(rec.GasBurnLog)
fmt.Println("rr.Read(rec.GasBurnLog: 2")
return rr.Err
}

Expand All @@ -92,6 +95,7 @@ func (rec *RequestReceipt) Write(w io.Writer) error {
if rec.Error != nil {
ww.Write(rec.Error)
}
ww.Write(rec.GasBurnLog)
return ww.Err
}

Expand All @@ -102,6 +106,7 @@ func (rec *RequestReceipt) String() string {
ret += fmt.Sprintf("Gas budget / burned / fee charged: %d / %d /%d\n", rec.GasBudget, rec.GasBurned, rec.GasFeeCharged)
ret += fmt.Sprintf("Storage deposit charged: %d\n", rec.SDCharged)
ret += fmt.Sprintf("Call data: %s\n", rec.Request)
ret += fmt.Sprintf("burn log: %s\n", rec.GasBurnLog)
return ret
}

Expand Down Expand Up @@ -134,6 +139,7 @@ func (rec *RequestReceipt) ToISCReceipt(resolvedError *isc.VMError) *isc.Receipt
BlockIndex: rec.BlockIndex,
RequestIndex: rec.RequestIndex,
ResolvedError: resolvedError.Error(),
GasBurnLog: rec.GasBurnLog,
}
}

Expand Down
8 changes: 4 additions & 4 deletions packages/vm/core/testcore/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ func TestDeposit(t *testing.T) {
err := ch.DepositBaseTokensToL2(100_000, sender)
require.NoError(t, err)

rec := ch.LastReceipt()
require.NotNil(t, rec)
t.Logf("========= receipt: %s", rec)
t.Logf("========= burn log:\n%s", rec.GasBurnLog)
receipt := ch.LastReceipt()
require.NotNil(t, receipt)
t.Logf("========= receipt: %s", receipt)
t.Logf("========= burn log:\n%s", receipt.GasBurnLog)
}

// allowance shouldn't allow you to bypass gas fees.
Expand Down
70 changes: 61 additions & 9 deletions packages/vm/gas/burnlog.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package gas

import (
"encoding/binary"
"fmt"
"io"
"strings"

"github.com/iotaledger/wasp/packages/util/rwutil"
)

type BurnRecord struct {
Expand All @@ -18,21 +22,69 @@ func NewGasBurnLog() *BurnLog {
return &BurnLog{Records: make([]BurnRecord, 0)}
}

func (h *BurnLog) Record(code BurnCode, gas uint64) {
if h != nil {
h.Records = append(h.Records, BurnRecord{code, gas})
func (l *BurnLog) Record(code BurnCode, gas uint64) {
if l != nil {
l.Records = append(l.Records, BurnRecord{code, gas})
}
}

func (h *BurnLog) String() string {
if h == nil {
func (l *BurnLog) Read(r io.Reader) error {
rr := rwutil.NewReader(r)
// rr.ReadN()
return rr.Err
}

func (l *BurnLog) Write(w io.Writer) error {
ww := rwutil.NewWriter(w)

// ww.WriteN(l)
return ww.Err
}

const burnLogRecordSize = 10 + 8

Check failure on line 44 in packages/vm/gas/burnlog.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `gofumpt`-ed (gofumpt)
const burnLogOffset = 4

func (l *BurnLog) Bytes() []byte {
b := make([]byte, 0, burnLogOffset+len(l.Records)*burnLogRecordSize)
binary.LittleEndian.PutUint32(b[0:burnLogOffset], uint32(len(l.Records)))
for i, record := range l.Records {
name := fmt.Sprintf("%10s", record.Code.Name())
length := len([]byte(name))
begin := burnLogOffset + i*burnLogRecordSize
copy(b[begin:begin+length], []byte(name))
binary.LittleEndian.PutUint64(b[begin+length:begin+length+8], record.GasBurned)
}
return b
}

func BurnLogFromBytes(b []byte) *BurnLog {
var burnLog BurnLog
recordLen := binary.LittleEndian.Uint32(b[0:burnLogOffset])

for i := 0; i < int(recordLen); i++ {
name := strings.TrimSpace(string(b[burnLogOffset+i*burnLogRecordSize : burnLogOffset+(i+1)*burnLogRecordSize-8]))

burnCode := BurnCodeFromName(name)
gasBurned := binary.LittleEndian.Uint64(b[burnLogOffset+(i+1)*burnLogRecordSize-8 : burnLogOffset+(i+1)*burnLogRecordSize])

burnLog.Records = append(burnLog.Records, BurnRecord{
Code: burnCode,
GasBurned: gasBurned,
})
}

return &burnLog
}

func (l *BurnLog) String() string {
if l == nil {
return "(no burn history)"
}
ret := make([]string, 0, len(h.Records)+2)
ret := make([]string, 0, len(l.Records)+2)
var total uint64
for i := range h.Records {
ret = append(ret, fmt.Sprintf("%10s: %d", h.Records[i].Code.Name(), h.Records[i].GasBurned))
total += h.Records[i].GasBurned
for i := range l.Records {
ret = append(ret, fmt.Sprintf("%10s: %d", l.Records[i].Code.Name(), l.Records[i].GasBurned))
total += l.Records[i].GasBurned
}
ret = append(ret, "---------------", fmt.Sprintf("%10s: %d", "TOTAL", total))
return strings.Join(ret, "\n")
Expand Down
23 changes: 23 additions & 0 deletions packages/vm/gas/burnlog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package gas_test

import (
"testing"

"github.com/iotaledger/wasp/packages/vm/gas"

Check failure on line 6 in packages/vm/gas/burnlog_test.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `goimports`-ed with -local github.com/iotaledger (goimports)
"github.com/stretchr/testify/require"
)

func TestBurnLogSerialization(t *testing.T) {
var burnLog gas.BurnLog
burnLog.Records = []gas.BurnRecord{
{
Code: gas.BurnCodeCallTargetNotFound,
GasBurned: 10,
},
{
Code: gas.BurnCodeUtilsHashingSha3,
GasBurned: 80,
},
}
require.Equal(t, &burnLog, gas.BurnLogFromBytes(burnLog.Bytes()))
}
14 changes: 13 additions & 1 deletion packages/vm/gas/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package gas

import "errors"
import (
"errors"
"fmt"
)

type BurnCode uint16

Expand All @@ -22,3 +25,12 @@ func (c BurnCode) Name() string {
}
return r.Name
}

func BurnCodeFromName(name string) BurnCode {
for burnCode := range burnTable {
if burnCode.Name() == name {
return burnCode
}
}
panic(fmt.Sprintf("name %s not exist", name))
}

0 comments on commit 0e70478

Please sign in to comment.