Skip to content

Commit

Permalink
add big endian and block timestamp for local executor (#40)
Browse files Browse the repository at this point in the history
* add big endian

* add block timestamp
  • Loading branch information
laizy authored May 26, 2022
1 parent 6c780f7 commit 1f575c8
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 12 deletions.
4 changes: 2 additions & 2 deletions abi/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ func (m *Method) ID() []byte {
return dst
}

func NewMethod(name string) (*Method, error) {
name, inputs, outputs, err := parseMethodSignature(name)
func NewMethod(sig string) (*Method, error) {
name, inputs, outputs, err := parseMethodSignature(sig)
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions executor/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func MessageFromTx(tx *web3.Transaction, checkNonce bool) Message {
nonce: tx.Nonce,
checkNonce: checkNonce,
data: tx.Input,
hash: tx.Hash(),
}
}

Expand Down
2 changes: 2 additions & 0 deletions jsonrpc/transport/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func (self *Local) Call(method string, out interface{}, params ...interface{}) e
_, receipt, err := self.exec.ExecuteTransaction(txn, executor.Eip155Context{
BlockHash: web3.Hash{},
Height: self.BlockNumber,
Timestamp: self.BlockNumber * 12,
})
if err != nil {
return err
Expand Down Expand Up @@ -146,6 +147,7 @@ func (self *Local) CallEvm(msg *web3.CallMsg) (*web3.ExecutionResult, error) {
res, _, err := self.exec.Call(CallMsg{msg}, executor.Eip155Context{
BlockHash: web3.Hash{},
Height: self.BlockNumber,
Timestamp: self.BlockNumber * 12,
})
if err != nil {
return nil, err
Expand Down
43 changes: 39 additions & 4 deletions utils/codec/zero_copy_sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ func (self *ZeroCopySink) grow(n int) int {
return l
}

func (self *ZeroCopySink) WriteBytes(p []byte) {
func (self *ZeroCopySink) WriteBytes(p []byte) *ZeroCopySink {
data := self.NextBytes(uint64(len(p)))
copy(data, p)
return self
}

func (self *ZeroCopySink) Size() uint64 { return uint64(len(self.buf)) }
Expand Down Expand Up @@ -113,9 +114,29 @@ func (self *ZeroCopySink) WriteUint32(data uint32) {
binary.LittleEndian.PutUint32(buf, data)
}

func (self *ZeroCopySink) WriteUint64(data uint64) {
func (self *ZeroCopySink) WriteUint64(data uint64) *ZeroCopySink {
buf := self.NextBytes(8)
binary.LittleEndian.PutUint64(buf, data)
return self
}

func (self *ZeroCopySink) WriteUint16BE(data uint16) *ZeroCopySink {
buf := self.NextBytes(2)
binary.BigEndian.PutUint16(buf, data)
return self
}

func (self *ZeroCopySink) WriteUint32BE(data uint32) *ZeroCopySink {
buf := self.NextBytes(4)
binary.BigEndian.PutUint32(buf, data)
return self
}

func (self *ZeroCopySink) WriteUint64BE(data uint64) *ZeroCopySink {
buf := self.NextBytes(8)
binary.BigEndian.PutUint64(buf, data)

return self
}

func (self *ZeroCopySink) WriteInt64(data int64) {
Expand All @@ -130,6 +151,18 @@ func (self *ZeroCopySink) WriteInt16(data int16) {
self.WriteUint16(uint16(data))
}

func (self *ZeroCopySink) WriteInt64BE(data int64) {
self.WriteUint64BE(uint64(data))
}

func (self *ZeroCopySink) WriteInt32BE(data int32) {
self.WriteUint32BE(uint32(data))
}

func (self *ZeroCopySink) WriteInt16BE(data int16) {
self.WriteUint16BE(uint16(data))
}

func (self *ZeroCopySink) WriteVarBytes(data []byte) (size uint64) {
l := uint64(len(data))
size = self.WriteVarUint(l) + l
Expand All @@ -142,12 +175,14 @@ func (self *ZeroCopySink) WriteString(data string) (size uint64) {
return self.WriteVarBytes([]byte(data))
}

func (self *ZeroCopySink) WriteAddress(addr web3.Address) {
func (self *ZeroCopySink) WriteAddress(addr web3.Address) *ZeroCopySink {
self.WriteBytes(addr[:])
return self
}

func (self *ZeroCopySink) WriteHash(hash web3.Hash) {
func (self *ZeroCopySink) WriteHash(hash web3.Hash) *ZeroCopySink {
self.WriteBytes(hash[:])
return self
}

func (self *ZeroCopySink) WriteVarUint(data uint64) (size uint64) {
Expand Down
38 changes: 32 additions & 6 deletions utils/codec/zero_copy_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ import (
var ErrIrregularData = errors.New("irregular data")

type ZeroCopySource struct {
s []byte
off uint64 // current reading index
s []byte
off uint64 // current reading index
byteOrder binary.ByteOrder
}

// Len returns the number of bytes of the unread portion of the
Expand Down Expand Up @@ -128,7 +129,7 @@ func (self *ZeroCopySource) NextUint16() (data uint16, eof bool) {
return
}

return binary.LittleEndian.Uint16(buf), eof
return self.byteOrder.Uint16(buf), eof
}

func (self *ZeroCopySource) NextUint32() (data uint32, eof bool) {
Expand All @@ -138,7 +139,7 @@ func (self *ZeroCopySource) NextUint32() (data uint32, eof bool) {
return
}

return binary.LittleEndian.Uint32(buf), eof
return self.byteOrder.Uint32(buf), eof
}

func (self *ZeroCopySource) ReadUint32() (data uint32, err error) {
Expand All @@ -150,6 +151,16 @@ func (self *ZeroCopySource) ReadUint32() (data uint32, err error) {
return data, nil
}

func (self *ZeroCopySource) ReadUint32BE() (data uint32, err error) {
origin := self.byteOrder
self.byteOrder = binary.BigEndian
defer func() {
self.byteOrder = origin
}()

return self.ReadUint32()
}

func (self *ZeroCopySource) ReadUint64() (data uint64, err error) {
data, eof := self.NextUint64()
if eof {
Expand All @@ -158,6 +169,15 @@ func (self *ZeroCopySource) ReadUint64() (data uint64, err error) {

return data, nil
}
func (self *ZeroCopySource) ReadUint64BE() (data uint64, err error) {
origin := self.byteOrder
self.byteOrder = binary.BigEndian
defer func() {
self.byteOrder = origin
}()

return self.ReadUint64()
}

func (self *ZeroCopySource) NextUint64() (data uint64, eof bool) {
var buf []byte
Expand All @@ -166,7 +186,7 @@ func (self *ZeroCopySource) NextUint64() (data uint64, eof bool) {
return
}

return binary.LittleEndian.Uint64(buf), eof
return self.byteOrder.Uint64(buf), eof
}

func (self *ZeroCopySource) NextInt32() (data int32, eof bool) {
Expand Down Expand Up @@ -300,4 +320,10 @@ func getVarUintSize(value uint64) uint64 {
}

// NewReader returns a new ZeroCopySource reading from b.
func NewZeroCopySource(b []byte) *ZeroCopySource { return &ZeroCopySource{b, 0} }
func NewZeroCopySource(b []byte) *ZeroCopySource {
return &ZeroCopySource{
s: b,
off: 0,
byteOrder: binary.LittleEndian,
}
}

0 comments on commit 1f575c8

Please sign in to comment.