Skip to content

Commit

Permalink
Added SHIP structures, fixup of BaseVariant.
Browse files Browse the repository at this point in the history
This is a rebase of the `fixes/variant` branch, without the large
test files.  We can add more focused tests when we use SHIP more.

* Add logging debug helpers, export Decode for convenience
* Fix transactionReceiptHandler reference from ship to eos
* Fix failedDtrxOp, fix Checksum on TransactionID variant
* fix table deltas when unmarshaled within block
  • Loading branch information
Stéphane Duchesneau authored and abourget committed Feb 27, 2020
1 parent cf71bc4 commit 7cd7a57
Show file tree
Hide file tree
Showing 10 changed files with 1,006 additions and 16 deletions.
4 changes: 4 additions & 0 deletions abidecoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func (a *ABI) DecodeTableRowTyped(tableType string, data []byte) ([]byte, error)
return a.decode(binaryDecoder, tableType)
}

func (a *ABI) Decode(binaryDecoder *Decoder, structName string) ([]byte, error) {
return a.decode(binaryDecoder, structName)
}

func (a *ABI) decode(binaryDecoder *Decoder, structName string) ([]byte, error) {
if loggingEnabled {
abiDecoderLog.Debug("decode struct", zap.String("name", structName))
Expand Down
6 changes: 6 additions & 0 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ func (e *Encoder) writeName(name Name) error {

func (e *Encoder) Encode(v interface{}) (err error) {
switch cv := v.(type) {
case BaseVariant:
err = e.writeUVarInt(int(cv.TypeID))
if err != nil {
return
}
return e.Encode(cv.Impl)
case Name:
return e.writeName(cv)
case AccountName:
Expand Down
8 changes: 2 additions & 6 deletions init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import (

func init() {
if os.Getenv("DEBUG") != "" {
coreLog, _ = zap.NewDevelopment()
encoderLog, _ = zap.NewDevelopment()
decoderLog, _ = zap.NewDevelopment()
abiEncoderLog, _ = zap.NewDevelopment()
abiDecoderLog, _ = zap.NewDevelopment()
loggingEnabled = true
logger, _ := zap.NewDevelopment()
EnableDebugLogging(logger)
}
}
9 changes: 9 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ var abiEncoderLog = zap.NewNop()
var abiDecoderLog = zap.NewNop()
var loggingEnabled = false

func EnableDebugLogging(l *zap.Logger) {
coreLog = l
encoderLog = l
decoderLog = l
abiEncoderLog = l
abiDecoderLog = l
loggingEnabled = true
}

func EnableCoreLogging() {
coreLog = newLogger(false)
enableLogging(coreLog)
Expand Down
2 changes: 1 addition & 1 deletion p2ptypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ type BlockSigningAuthority struct {
BaseVariant
}

var blockSigningVariantFactoryImplMap = map[uint]VariantImplFactory{
var blockSigningVariantFactoryImplMap = map[uint32]VariantImplFactory{
BlockSigningAuthorityV0Type: func() interface{} { return new(BlockSigningAuthorityV0) },
}

Expand Down
167 changes: 167 additions & 0 deletions ship/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package ship

import (
"github.com/eoscanada/eos-go"
"github.com/eoscanada/eos-go/ecc"
)

// State History Plugin Requests

type GetStatusRequestV0 struct {
}

type GetBlocksAckRequestV0 struct {
NumMessages uint32
}

type GetBlocksRequestV0 struct {
StartBlockNum uint32
EndBlockNum uint32
MaxMessagesInFlight uint32
HavePositions []*BlockPosition
IrreversibleOnly bool
FetchBlock bool
FetchTraces bool
FetchDeltas bool
}

// State History Plugin Results
type GetStatusResultV0 struct {
Head *BlockPosition
LastIrreversible *BlockPosition
TraceBeginBlock uint32
TraceEndBlock uint32
ChainStateBeginBlock uint32
ChainStateEndBlock uint32
}

type GetBlocksResultV0 struct {
Head *BlockPosition
LastIrreversible *BlockPosition
ThisBlock *BlockPosition `eos:"optional"`
PrevBlock *BlockPosition `eos:"optional"`
Block *SignedBlockBytes `eos:"optional"`
Traces *TransactionTraceArray `eos:"optional"`
Deltas *TableDeltaArray `eos:"optional"`
}

// State History Plugin version of EOS structs
type BlockPosition struct {
BlockNum uint32
BlockID eos.Checksum256
}

type Row struct {
Present bool
Data []byte
}

type ActionTraceV0 struct {
ActionOrdinal eos.Varuint32
CreatorActionOrdinal eos.Varuint32
Receipt *ActionReceipt `eos:"optional"`
Receiver eos.Name
Act *Action
ContextFree bool
Elapsed int64
Console string
AccountRamDeltas []*eos.AccountRAMDelta
Except string `eos:"optional"`
ErrorCode uint64 `eos:"optional"`
}

type Action struct {
Account eos.AccountName
Name eos.ActionName
Authorization []eos.PermissionLevel
Data []byte
}

type ActionReceiptV0 struct {
Receiver eos.Name
ActDigest eos.Checksum256
GlobalSequence uint64
RecvSequence uint64
AuthSequence []AccountAuthSequence
CodeSequence eos.Varuint32
ABISequence eos.Varuint32
}

type AccountAuthSequence struct {
Account eos.Name
Sequence uint64
}

type TableDeltaV0 struct {
Name string
Rows []Row
}

type PartialTransactionV0 struct {
Expiration uint32
RefBlockNum uint16
RefBlockPrefix uint32
MaxNetUsageWords eos.Varuint32
MaxCpuUsageMs uint8
DelaySec eos.Varuint32
TransactionExtensions []*Extension
Signatures []ecc.Signature
ContextFreeData []byte
}

type TransactionTraceV0 struct {
ID eos.Checksum256 `json:"id"`
Status eos.TransactionStatus
CPUUsageUS uint32 `json:"cpu_usage_us"`
NetUsageWords eos.Varuint32 `json:"net_usage_words"`
Elapsed eos.Int64 `json:"elapsed"`
NetUsage uint64 `json:"net_usage"`
Scheduled bool `json:"scheduled"`
ActionTraces []*ActionTrace `json:"action_traces"`
AccountDelta *eos.AccountRAMDelta `json:"account_delta" eos:"optional"`
Except string `json:"except" eos:"optional"`
ErrorCode uint64 `json:"error_code" eos:"optional"`
FailedDtrxTrace *TransactionTrace `json:"failed_dtrx_trace" eos:"optional"`
Partial *PartialTransaction `json:"partial" eos:"optional"`
}

type SignedBlockHeader struct {
eos.BlockHeader
ProducerSignature ecc.Signature // no pointer!!
}

type TransactionReceipt struct {
eos.TransactionReceiptHeader
Trx *TransactionVariant
}

//type TransactionID eos.Checksum256

type SignedBlock struct {
SignedBlockHeader
Transactions []*TransactionReceipt
BlockExtensions []*Extension
}

type SignedBlockBytes SignedBlock

func (s *SignedBlockBytes) AsSignedBlock() *SignedBlock {
if s == nil {
return nil
}
ss := SignedBlock(*s)
return &ss
}

func (s *SignedBlockBytes) UnmarshalBinary(decoder *eos.Decoder) error {
data, err := decoder.ReadByteArray()
if err != nil {
return err
}
return eos.UnmarshalBinary(data, s)
}

type Extension struct {
Type uint16
Data []byte
}
50 changes: 50 additions & 0 deletions ship/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package ship

import (
"fmt"

"github.com/eoscanada/eos-go"
)

func NewGetBlocksAck(num uint32) []byte {
myReq := &Request{
BaseVariant: eos.BaseVariant{
TypeID: GetBlocksAckRequestV0Type,
Impl: &GetBlocksAckRequestV0{
NumMessages: num,
},
},
}
bytes, err := eos.MarshalBinary(myReq)
if err != nil {
panic(err)
}

return bytes
}

func NewRequest(req *GetBlocksRequestV0) []byte {
myReq := &Request{
BaseVariant: eos.BaseVariant{
TypeID: GetBlocksRequestV0Type,
Impl: req,
},
}
bytes, err := eos.MarshalBinary(myReq)
if err != nil {
panic(err)
}

return bytes
}

func ParseGetBlockResultV0(in []byte) (*GetBlocksResultV0, error) {
variant := &Result{}
if err := eos.UnmarshalBinary(in, &variant); err != nil {
return nil, err
}
if variant.TypeID != GetBlocksResultV0Type {
return nil, fmt.Errorf("invalid response type: %d", variant.TypeID)
}
return variant.Impl.(*GetBlocksResultV0), nil
}
Loading

0 comments on commit 7cd7a57

Please sign in to comment.