-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Update tx indexer to include tx action outputs #1597
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,14 +4,14 @@ | |
package indexer | ||
|
||
import ( | ||
"encoding/binary" | ||
"errors" | ||
"path/filepath" | ||
|
||
"github.com/ava-labs/avalanchego/database" | ||
"github.com/ava-labs/avalanchego/ids" | ||
|
||
"github.com/ava-labs/hypersdk/chain" | ||
"github.com/ava-labs/hypersdk/codec" | ||
"github.com/ava-labs/hypersdk/consts" | ||
"github.com/ava-labs/hypersdk/event" | ||
"github.com/ava-labs/hypersdk/fees" | ||
|
@@ -25,9 +25,6 @@ const ( | |
) | ||
|
||
var ( | ||
failureByte = byte(0x0) | ||
successByte = byte(0x1) | ||
|
||
_ event.SubscriptionFactory[*chain.StatefulBlock] = (*subscriptionFactory)(nil) | ||
_ event.Subscription[*chain.StatefulBlock] = (*txDBIndexer)(nil) | ||
) | ||
|
@@ -103,6 +100,7 @@ func (t *txDBIndexer) Accept(blk *chain.StatefulBlock) error { | |
result.Success, | ||
result.Units, | ||
result.Fee, | ||
result.Outputs, | ||
); err != nil { | ||
return err | ||
} | ||
|
@@ -122,36 +120,54 @@ func (*txDBIndexer) storeTransaction( | |
success bool, | ||
units fees.Dimensions, | ||
fee uint64, | ||
outputs [][]byte, | ||
) error { | ||
v := make([]byte, consts.Uint64Len+1+fees.DimensionsLen+consts.Uint64Len) | ||
binary.BigEndian.PutUint64(v, uint64(timestamp)) | ||
if success { | ||
v[consts.Uint64Len] = successByte | ||
} else { | ||
v[consts.Uint64Len] = failureByte | ||
outputLength := 1 // Single byte containing number of outputs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: |
||
for _, output := range outputs { | ||
outputLength += consts.Uint32Len + len(output) | ||
} | ||
txResultLength := consts.Uint64Len + 1 + fees.DimensionsLen + consts.Uint64Len + outputLength | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: we use an exact Alternative would be to use |
||
|
||
writer := codec.NewWriter(txResultLength, txResultLength) | ||
writer.PackUint64(uint64(timestamp)) | ||
writer.PackBool(success) | ||
writer.PackFixedBytes(units.Bytes()) | ||
writer.PackUint64(fee) | ||
writer.PackByte(byte(len(outputs))) | ||
for _, output := range outputs { | ||
writer.PackBytes(output) | ||
} | ||
copy(v[consts.Uint64Len+1:], units.Bytes()) | ||
binary.BigEndian.PutUint64(v[consts.Uint64Len+1+fees.DimensionsLen:], fee) | ||
return batch.Put(txID[:], v) | ||
if err := writer.Err(); err != nil { | ||
return err | ||
} | ||
return batch.Put(txID[:], writer.Bytes()) | ||
} | ||
|
||
func (t *txDBIndexer) GetTransaction(txID ids.ID) (bool, int64, bool, fees.Dimensions, uint64, error) { | ||
func (t *txDBIndexer) GetTransaction(txID ids.ID) (bool, int64, bool, fees.Dimensions, uint64, [][]byte, error) { | ||
v, err := t.db.Get(txID[:]) | ||
if errors.Is(err, database.ErrNotFound) { | ||
return false, 0, false, fees.Dimensions{}, 0, nil | ||
return false, 0, false, fees.Dimensions{}, 0, nil, nil | ||
} | ||
if err != nil { | ||
return false, 0, false, fees.Dimensions{}, 0, err | ||
return false, 0, false, fees.Dimensions{}, 0, nil, err | ||
} | ||
reader := codec.NewReader(v, consts.NetworkSizeLimit) | ||
timestamp := reader.UnpackUint64(true) | ||
success := reader.UnpackBool() | ||
dimensionsBytes := make([]byte, fees.DimensionsLen) | ||
reader.UnpackFixedBytes(fees.DimensionsLen, &dimensionsBytes) | ||
fee := reader.UnpackUint64(true) | ||
numOutputs := int(reader.UnpackByte()) | ||
outputs := make([][]byte, numOutputs) | ||
for i := range outputs { | ||
outputs[i] = reader.UnpackLimitedBytes(consts.NetworkSizeLimit) | ||
} | ||
timestamp := int64(binary.BigEndian.Uint64(v)) | ||
success := true | ||
if v[consts.Uint64Len] == failureByte { | ||
success = false | ||
if err := reader.Err(); err != nil { | ||
return false, 0, false, fees.Dimensions{}, 0, nil, err | ||
} | ||
d, err := fees.UnpackDimensions(v[consts.Uint64Len+1 : consts.Uint64Len+1+fees.DimensionsLen]) | ||
dimensions, err := fees.UnpackDimensions(dimensionsBytes) | ||
if err != nil { | ||
return false, 0, false, fees.Dimensions{}, 0, err | ||
return false, 0, false, fees.Dimensions{}, 0, nil, err | ||
} | ||
fee := binary.BigEndian.Uint64(v[consts.Uint64Len+1+fees.DimensionsLen:]) | ||
return true, timestamp, success, d, fee, nil | ||
return true, int64(timestamp), success, dimensions, fee, outputs, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package codec | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestBytesHex(t *testing.T) { | ||
require := require.New(t) | ||
b := []byte{1, 2, 3, 4, 5} | ||
wrappedBytes := Bytes(b) | ||
|
||
marshalledBytes, err := wrappedBytes.MarshalText() | ||
require.NoError(err) | ||
|
||
var unmarshalledBytes Bytes | ||
require.NoError(unmarshalledBytes.UnmarshalText(marshalledBytes)) | ||
require.Equal(b, []byte(unmarshalledBytes)) | ||
|
||
jsonMarshalledBytes, err := json.Marshal(wrappedBytes) | ||
require.NoError(err) | ||
|
||
var jsonUnmarshalledBytes Bytes | ||
require.NoError(json.Unmarshal(jsonMarshalledBytes, &jsonUnmarshalledBytes)) | ||
require.Equal(b, []byte(jsonUnmarshalledBytes)) | ||
} |
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.
Uses wrapped
codec.Bytes
to provide hex format