-
Notifications
You must be signed in to change notification settings - Fork 503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
5570/xdrill contract events #5583
Changes from all commits
a1f0f73
f1f97a9
36cfcbc
d064296
15fe1e5
d45b0fd
e9de99c
40d5057
8942168
b5feb2c
4aedfdb
a5bd927
0b62fe2
cce0cfe
dab3bd9
219391d
58fcfe2
18dd712
314b605
d385526
be020c9
e2ce9bd
69f9bf2
44e627f
10b4031
93f9639
b890631
47ecec8
c2a3616
019a3fd
ee153f8
16d94ff
e9d5511
3bc82f4
21dd54e
b6851fc
3d7876c
c8c4caf
dd68880
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package diagnosticevent | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/chowbao/go-stellar-xdr-json/xdr2json" | ||
Check failure on line 7 in ingest/diagnostic_event/diagnostic_event.go
|
||
"github.com/stellar/go/strkey" | ||
"github.com/stellar/go/xdr" | ||
) | ||
|
||
func Successful(d xdr.DiagnosticEvent) bool { | ||
return d.InSuccessfulContractCall | ||
} | ||
|
||
func Type(d xdr.DiagnosticEvent) int32 { | ||
return int32(d.Event.Type) | ||
} | ||
|
||
func ContractID(d xdr.DiagnosticEvent) (string, bool, error) { | ||
if d.Event.ContractId == nil { | ||
return "", false, nil | ||
} | ||
|
||
var err error | ||
var contractIdByte []byte | ||
var contractIDString string | ||
contractId := *d.Event.ContractId | ||
contractIdByte, err = contractId.MarshalBinary() | ||
if err != nil { | ||
return "", false, nil | ||
} | ||
contractIDString, err = strkey.Encode(strkey.VersionByteContract, contractIdByte) | ||
if err != nil { | ||
return "", false, nil | ||
} | ||
|
||
return contractIDString, true, nil | ||
} | ||
|
||
func Topics(d xdr.DiagnosticEvent) ([]interface{}, error) { | ||
topics, err := GetEventTopics(d) | ||
if err != nil { | ||
return []interface{}{}, err | ||
} | ||
|
||
return serializeScValArray(topics) | ||
} | ||
|
||
func Data(d xdr.DiagnosticEvent) (interface{}, error) { | ||
data, err := GetEventData(d) | ||
if err != nil { | ||
return []interface{}{}, err | ||
} | ||
|
||
return serializeScVal(data) | ||
} | ||
|
||
func GetEventTopics(d xdr.DiagnosticEvent) ([]xdr.ScVal, error) { | ||
switch d.Event.Body.V { | ||
case 0: | ||
contractEventV0 := d.Event.Body.MustV0() | ||
return contractEventV0.Topics, nil | ||
default: | ||
return []xdr.ScVal{}, fmt.Errorf("unsupported event body version: " + string(d.Event.Body.V)) | ||
} | ||
} | ||
|
||
func GetEventData(d xdr.DiagnosticEvent) (xdr.ScVal, error) { | ||
switch d.Event.Body.V { | ||
case 0: | ||
contractEventV0 := d.Event.Body.MustV0() | ||
return contractEventV0.Data, nil | ||
default: | ||
return xdr.ScVal{}, fmt.Errorf("unsupported event body version: " + string(d.Event.Body.V)) | ||
} | ||
} | ||
|
||
func serializeScVal(scVal xdr.ScVal) (interface{}, error) { | ||
var serializedDataDecoded interface{} | ||
serializedDataDecoded = "n/a" | ||
|
||
if _, ok := scVal.ArmForSwitch(int32(scVal.Type)); ok { | ||
var err error | ||
var raw []byte | ||
var jsonMessage json.RawMessage | ||
raw, err = scVal.MarshalBinary() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
jsonMessage, err = xdr2json.ConvertBytes(xdr.ScVal{}, raw) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
serializedDataDecoded = jsonMessage | ||
} | ||
|
||
return serializedDataDecoded, nil | ||
} | ||
|
||
func serializeScValArray(scVals []xdr.ScVal) ([]interface{}, error) { | ||
dataDecoded := make([]interface{}, 0, len(scVals)) | ||
|
||
for _, scVal := range scVals { | ||
serializedDataDecoded, err := serializeScVal(scVal) | ||
if err != nil { | ||
return nil, err | ||
} | ||
dataDecoded = append(dataDecoded, serializedDataDecoded) | ||
} | ||
|
||
return dataDecoded, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package ledger | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/stellar/go/xdr" | ||
) | ||
|
||
func Sequence(l xdr.LedgerCloseMeta) uint32 { | ||
return uint32(l.LedgerHeaderHistoryEntry().Header.LedgerSeq) | ||
} | ||
|
||
func Hash(l xdr.LedgerCloseMeta) string { | ||
return l.LedgerHeaderHistoryEntry().Hash.HexString() | ||
} | ||
|
||
func PreviousHash(l xdr.LedgerCloseMeta) string { | ||
return l.PreviousLedgerHash().HexString() | ||
} | ||
|
||
func CloseTime(l xdr.LedgerCloseMeta) int64 { | ||
return l.LedgerCloseTime() | ||
} | ||
|
||
func ClosedAt(l xdr.LedgerCloseMeta) time.Time { | ||
return time.Unix(l.LedgerCloseTime(), 0).UTC() | ||
} | ||
|
||
func TotalCoins(l xdr.LedgerCloseMeta) int64 { | ||
return int64(l.LedgerHeaderHistoryEntry().Header.TotalCoins) | ||
} | ||
|
||
func FeePool(l xdr.LedgerCloseMeta) int64 { | ||
return int64(l.LedgerHeaderHistoryEntry().Header.FeePool) | ||
} | ||
|
||
func BaseFee(l xdr.LedgerCloseMeta) uint32 { | ||
return uint32(l.LedgerHeaderHistoryEntry().Header.BaseFee) | ||
} | ||
|
||
func BaseReserve(l xdr.LedgerCloseMeta) uint32 { | ||
return uint32(l.LedgerHeaderHistoryEntry().Header.BaseReserve) | ||
} | ||
|
||
func MaxTxSetSize(l xdr.LedgerCloseMeta) uint32 { | ||
return uint32(l.LedgerHeaderHistoryEntry().Header.MaxTxSetSize) | ||
} | ||
|
||
func LedgerVersion(l xdr.LedgerCloseMeta) uint32 { | ||
return uint32(l.LedgerHeaderHistoryEntry().Header.LedgerVersion) | ||
} | ||
|
||
func SorobanFeeWrite1Kb(l xdr.LedgerCloseMeta) (int64, bool) { | ||
lcmV1, ok := l.GetV1() | ||
if !ok { | ||
return 0, false | ||
} | ||
|
||
extV1, ok := lcmV1.Ext.GetV1() | ||
if !ok { | ||
return 0, false | ||
} | ||
|
||
return int64(extV1.SorobanFeeWrite1Kb), true | ||
} | ||
|
||
func TotalByteSizeOfBucketList(l xdr.LedgerCloseMeta) (uint64, bool) { | ||
lcmV1, ok := l.GetV1() | ||
if !ok { | ||
return 0, false | ||
} | ||
|
||
return uint64(lcmV1.TotalByteSizeOfBucketList), true | ||
} | ||
|
||
func NodeID(l xdr.LedgerCloseMeta) (string, error) { | ||
LedgerCloseValueSignature, ok := l.LedgerHeaderHistoryEntry().Header.ScpValue.Ext.GetLcValueSignature() | ||
if !ok { | ||
return "", fmt.Errorf("could not get LedgerCloseValueSignature") | ||
|
||
} | ||
return LedgerCloseValueSignature.NodeId.GetAddress() | ||
} | ||
|
||
func Signature(l xdr.LedgerCloseMeta) (string, bool) { | ||
LedgerCloseValueSignature, ok := l.LedgerHeaderHistoryEntry().Header.ScpValue.Ext.GetLcValueSignature() | ||
if !ok { | ||
return "", false | ||
} | ||
|
||
return base64.StdEncoding.EncodeToString(LedgerCloseValueSignature.Signature), true | ||
} | ||
|
||
// TransactionCounts calculates and returns the number of successful and total transactions | ||
func TransactionCounts(l xdr.LedgerCloseMeta) (successTxCount, totalTxCount uint32) { | ||
transactions := l.TransactionEnvelopes() | ||
results, err := l.TxProcessing() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
txCount := len(transactions) | ||
if txCount != len(results) { | ||
panic("transaction count and number of TransactionResultMeta not equal") | ||
} | ||
|
||
for i := 0; i < txCount; i++ { | ||
if results[i].Result.Successful() { | ||
successTxCount++ | ||
} | ||
} | ||
|
||
return successTxCount, uint32(txCount) | ||
} | ||
|
||
// OperationCounts calculates and returns the number of successful operations and the total operations within | ||
// a LedgerCloseMeta | ||
func OperationCounts(l xdr.LedgerCloseMeta) (successfulOperationCount, totalOperationCount uint32) { | ||
transactions := l.TransactionEnvelopes() | ||
results, err := l.TxProcessing() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for i, result := range results { | ||
operations := transactions[i].OperationsCount() | ||
totalOperationCount += operations | ||
|
||
// for successful transactions, the operation count is based on the operations results slice | ||
if result.Result.Successful() { | ||
operationResults, ok := result.Result.OperationResults() | ||
if !ok { | ||
panic("could not get OperationResults") | ||
} | ||
|
||
successfulOperationCount += uint32(len(operationResults)) | ||
} | ||
} | ||
|
||
return successfulOperationCount, totalOperationCount | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: move xdr2json to it's own repo stellar/stellar-rpc#350