-
Notifications
You must be signed in to change notification settings - Fork 45
/
singlet_contract_abi.go
118 lines (90 loc) · 2.83 KB
/
singlet_contract_abi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package statedb
import (
"fmt"
pbcodec "github.com/dfuse-io/dfuse-eosio/pb/dfuse/eosio/codec/v1"
pbstatedb "github.com/dfuse-io/dfuse-eosio/pb/dfuse/eosio/statedb/v1"
"github.com/eoscanada/eos-go"
"github.com/eoscanada/eos-go/system"
"github.com/golang/protobuf/proto"
"github.com/streamingfast/fluxdb"
)
const abiCollection = 0xA000
const abiName = "abi"
func init() {
fluxdb.RegisterSingletFactory(abiCollection, abiName, func(identifier []byte) (fluxdb.Singlet, error) {
if len(identifier) < 8 {
return nil, fluxdb.ErrInvalidKeyLengthAtLeast("abi singlet identifier", 8, len(identifier))
}
return ContractABISinglet(identifier[0:8]), nil
})
}
type ContractABISinglet []byte
func NewContractABISinglet(contract string) ContractABISinglet {
return ContractABISinglet(standardNameToBytes(contract))
}
func (s ContractABISinglet) Collection() uint16 {
return abiCollection
}
func (s ContractABISinglet) Identifier() []byte {
return []byte(s)
}
func (s ContractABISinglet) Entry(height uint64, data []byte) (fluxdb.SingletEntry, error) {
return &ContractABIEntry{baseEntry(s, height, data)}, nil
}
func (s ContractABISinglet) Contract() string {
return bytesToName(s)
}
func (s ContractABISinglet) String() string {
return abiName + ":" + bytesToName(s)
}
type ContractABIEntry struct {
fluxdb.BaseSingletEntry
}
func NewContractABIEntry(blockNum uint64, actionTrace *pbcodec.ActionTrace) (entry *ContractABIEntry, err error) {
if !actionTrace.Action.HasJSONDecodedData() {
return nil, nil
}
var setABI *system.SetABI
if err := actionTrace.Action.UnmarshalData(&setABI); err != nil {
return nil, err
}
var value []byte
if len(setABI.ABI) > 0 {
pb := pbstatedb.ContractABIValue{RawAbi: setABI.ABI}
if value, err = proto.Marshal(&pb); err != nil {
return nil, fmt.Errorf("marshal proto: %w", err)
}
}
singlet := ContractABISinglet(nameaToBytes(setABI.Account))
return &ContractABIEntry{baseEntry(singlet, blockNum, value)}, nil
}
func (r *ContractABIEntry) Contract() string {
return r.Singlet().(ContractABISinglet).Contract()
}
type ContractABIOption bool
var ContractABIPackedOnly = ContractABIOption(true)
func (r *ContractABIEntry) ABI(options ...ContractABIOption) (abi *eos.ABI, rawBytes []byte, err error) {
if r == nil {
return nil, nil, nil
}
pb := pbstatedb.ContractABIValue{}
if err := proto.Unmarshal(r.Value(), &pb); err != nil {
return nil, nil, err
}
rawABI := pb.RawAbi
if len(options) > 0 && options[0] == ContractABIPackedOnly {
return nil, rawABI, nil
}
abi = new(eos.ABI)
if err := eos.UnmarshalBinary(rawABI, abi); err != nil {
return nil, rawABI, errABIUnmarshal
}
return abi, rawABI, nil
}
func (r *ContractABIEntry) ToProto() (proto.Message, error) {
pb := &pbstatedb.ContractABIValue{}
if err := proto.Unmarshal(r.Value(), pb); err != nil {
return nil, err
}
return pb, nil
}