Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions cmd/chain-receipts/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,10 @@ func listener(provider *ethrpc.Provider, monitorOptions ethmonitor.Options, rece
// Find specific meta transaction -- note: this is not the "transaction hash",
// this is a sub-transaction where the id is emitted as an event.
FilterMetaTransactionID := func(metaTxnID ethkit.Hash) ethreceipts.FilterQuery {
return ethreceipts.FilterLogs(func(logs []*types.Log) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you write a little benchmark separately to see what the performance effects are of doing this..? I'm curious if this will save us memory in practice..?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After writing a benchmark neither pointer removal or sonic help with performance.

return ethreceipts.FilterLogs(func(logs []types.Log) bool {
for _, log := range logs {
isTxExecuted := IsTxExecutedEvent(log, metaTxnID)
isTxFailed := IsTxFailedEvent(log, metaTxnID)
if isTxExecuted || isTxFailed {
// found the sequence meta txn
// found the sequence meta txn
if IsTxExecutedEvent(log, metaTxnID) || IsTxFailedEvent(log, metaTxnID) {
return true
}
}
Expand All @@ -116,7 +114,7 @@ func listener(provider *ethrpc.Provider, monitorOptions ethmonitor.Options, rece

// Find any Sequence meta txns
FilterMetaTransactionAny := func() ethreceipts.FilterQuery {
return ethreceipts.FilterLogs(func(logs []*types.Log) bool {
return ethreceipts.FilterLogs(func(logs []types.Log) bool {
foundNonceEvent := false
for _, log := range logs {
if len(log.Topics) > 0 && log.Topics[0] == NonceChangeEventSig {
Expand Down Expand Up @@ -208,13 +206,13 @@ func MustEncodeSig(str string) common.Hash {
return crypto.Keccak256Hash([]byte(str))
}

func IsTxExecutedEvent(log *types.Log, hash common.Hash) bool {
func IsTxExecutedEvent(log types.Log, hash common.Hash) bool {
return len(log.Topics) == 0 &&
len(log.Data) == 32 &&
bytes.Equal(log.Data, hash[:])
}

func IsTxFailedEvent(log *types.Log, hash common.Hash) bool {
func IsTxFailedEvent(log types.Log, hash common.Hash) bool {
return len(log.Topics) == 1 &&
log.Topics[0] == TxFailedEventSig &&
bytes.HasPrefix(log.Data, hash[:])
Expand Down
6 changes: 4 additions & 2 deletions cmd/chain-watch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"context"
"encoding/json"
"fmt"
"log"
"math/big"
Expand All @@ -13,7 +12,10 @@ import (

"github.com/0xsequence/ethkit/ethmonitor"
"github.com/0xsequence/ethkit/ethrpc"
"github.com/bytedance/sonic"

"github.com/0xsequence/ethkit/util"

rediscache "github.com/goware/cachestore-redis"
cachestore "github.com/goware/cachestore2"
"github.com/goware/logger"
Expand Down Expand Up @@ -201,7 +203,7 @@ func chainWatch(provider *ethrpc.Provider, monitorOptions ethmonitor.Options) (*
// of objects, one after another.
// Or... we can write [event1, event2,event3],[event,event5],[event6],...
// to the disk, and this would be fine too.
d, _ := json.Marshal(events)
d, _ := sonic.ConfigFastest.Marshal(events)
writeToFile(snapshotFile, d)
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/ethkit/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func NewHeader(b *types.Block) *Header {
WithdrawalsHash: b.Header().WithdrawalsHash,
Size: b.Header().Size(),
// TotalDifficulty: b.Difficulty(),
TransactionsHash: TransactionsHash(*b),
TransactionsHash: TransactionsHash(b),
}
}

Expand All @@ -188,7 +188,7 @@ func (h *Header) String() string {
}

// TransactionsHash returns a list of transaction hash starting from a list of transactions contained in a block.
func TransactionsHash(block types.Block) []common.Hash {
func TransactionsHash(block *types.Block) []common.Hash {
txsh := make([]common.Hash, len(block.Transactions()))

for i, tx := range block.Transactions() {
Expand Down
9 changes: 5 additions & 4 deletions cmd/ethkit/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"bytes"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
"text/tabwriter"

"github.com/bytedance/sonic"
)

type printableFormat struct {
Expand All @@ -29,7 +30,7 @@ type Printable map[string]any

// PrettyJSON prints an object in "prettified" JSON format
func PrettyJSON(toJSON any) (*string, error) {
b, err := json.MarshalIndent(toJSON, "", " ")
b, err := sonic.ConfigFastest.MarshalIndent(toJSON, "", " ")
if err != nil {
return nil, err
}
Expand All @@ -45,11 +46,11 @@ func PrettyJSON(toJSON any) (*string, error) {

// FromStruct converts a struct into a Printable using, when available, JSON field names as keys
func (p *Printable) FromStruct(input any) error {
bytes, err := json.Marshal(input)
bytes, err := sonic.ConfigFastest.Marshal(input)
if err != nil {
return err
}
if err := json.Unmarshal(bytes, &p); err != nil {
if err := sonic.ConfigFastest.Unmarshal(bytes, &p); err != nil {
return err
}

Expand Down
11 changes: 6 additions & 5 deletions cmd/ethkit/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"bufio"
"encoding/json"
"errors"
"fmt"
"log"
Expand All @@ -13,8 +12,10 @@ import (
"github.com/0xsequence/ethkit/ethwallet"
"github.com/0xsequence/ethkit/go-ethereum/accounts/keystore"
"github.com/0xsequence/ethkit/go-ethereum/common"
"github.com/bytedance/sonic"

"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/term"
)

func init() {
Expand Down Expand Up @@ -90,7 +91,7 @@ func (c *wallet) Run(cmd *cobra.Command, args []string) {
log.Fatal(err)
}
keyFile := walletKeyFile{}
err = json.Unmarshal(data, &keyFile)
err = sonic.ConfigFastest.Unmarshal(data, &keyFile)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -219,7 +220,7 @@ func (c *wallet) createNew() error {
Client: fmt.Sprintf("ethkit/%s - github.com/0xsequence/ethkit", VERSION),
}

data, err := json.MarshalIndent(keyFile, "", " ")
data, err := sonic.ConfigFastest.MarshalIndent(keyFile, "", " ")
if err != nil {
return err
}
Expand Down Expand Up @@ -261,7 +262,7 @@ func fileExists(filename string) bool {

func readSecretInput(prompt string) ([]byte, error) {
fmt.Print(prompt)
password, err := terminal.ReadPassword(int(syscall.Stdin))
password, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions ethartifact/ethartifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/0xsequence/ethkit/go-ethereum/accounts/abi"
"github.com/0xsequence/ethkit/go-ethereum/common"
"github.com/bytedance/sonic"
)

type Artifact struct {
Expand All @@ -27,7 +28,7 @@ func (a Artifact) Decode(result interface{}, method string, data []byte) error {

func ParseArtifactJSON(artifactJSON string) (Artifact, error) {
var rawArtifact RawArtifact
err := json.Unmarshal([]byte(artifactJSON), &rawArtifact)
err := sonic.ConfigFastest.Unmarshal([]byte(artifactJSON), &rawArtifact)
if err != nil {
return Artifact{}, err
}
Expand Down Expand Up @@ -77,7 +78,7 @@ func ParseArtifactFile(path string) (RawArtifact, error) {
}

var artifact RawArtifact
err = json.Unmarshal(filedata, &artifact)
err = sonic.ConfigFastest.Unmarshal(filedata, &artifact)
if err != nil {
return RawArtifact{}, err
}
Expand Down
4 changes: 2 additions & 2 deletions ethcoder/abi_helpers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ethcoder

import (
"encoding/json"
"errors"
"fmt"
"math/big"
Expand All @@ -11,6 +10,7 @@ import (
"github.com/0xsequence/ethkit/go-ethereum/accounts/abi"
"github.com/0xsequence/ethkit/go-ethereum/common"
"github.com/0xsequence/ethkit/go-ethereum/common/hexutil"
"github.com/bytedance/sonic"
)

func ABIPackArguments(argTypes []string, argValues []interface{}) ([]byte, error) {
Expand Down Expand Up @@ -459,7 +459,7 @@ func ABIUnmarshalStringValuesAny(argTypes []string, stringValues []any) ([]any,
switch v := v.(type) {
case string:
// v is string array, ie. `["1","2","3"]`
err = json.Unmarshal([]byte(v), &stringValues)
err = sonic.ConfigFastest.Unmarshal([]byte(v), &stringValues)
if err != nil {
return nil, fmt.Errorf("ethcoder: value at position %d is invalid. failed to unmarshal json string array '%s'", i, v)
}
Expand Down
13 changes: 7 additions & 6 deletions ethcoder/abi_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package ethcoder

import (
"encoding/json"
"math/big"
"reflect"
"testing"

"github.com/0xsequence/ethkit/go-ethereum/accounts/abi"
"github.com/0xsequence/ethkit/go-ethereum/common"
"github.com/0xsequence/ethkit/go-ethereum/common/hexutil"
"github.com/bytedance/sonic"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -560,10 +561,10 @@ func TestEncodeContractCall(t *testing.T) {
// Args: []any{"mundo"},
// }

// net1jsn, err := json.Marshal(nestedEncodeType1)
// net1jsn, err := sonic.ConfigFastest.Marshal(nestedEncodeType1)
// require.Nil(t, err)

// net2jsn, err := json.Marshal(nestedEncodeType2)
// net2jsn, err := sonic.ConfigFastest.Marshal(nestedEncodeType2)
// require.Nil(t, err)

t.Run("nested transferFrom, not named", func(t *testing.T) {
Expand Down Expand Up @@ -653,7 +654,7 @@ func TestEncodeContractCall(t *testing.T) {
}`

var contractCall ContractCallDef
err := json.Unmarshal([]byte(jsonContractCall), &contractCall)
err := sonic.ConfigFastest.Unmarshal([]byte(jsonContractCall), &contractCall)
require.NoError(t, err)

res, err := EncodeContractCall(contractCall)
Expand All @@ -670,7 +671,7 @@ func TestEncodeContractCall(t *testing.T) {
}`

var contractCall ContractCallDef
err := json.Unmarshal([]byte(jsonContractCall), &contractCall)
err := sonic.ConfigFastest.Unmarshal([]byte(jsonContractCall), &contractCall)
require.NoError(t, err)

res, err := EncodeContractCall(contractCall)
Expand All @@ -687,7 +688,7 @@ func TestEncodeContractCall(t *testing.T) {
}`

var contractCall ContractCallDef
err := json.Unmarshal([]byte(jsonContractCall), &contractCall)
err := sonic.ConfigFastest.Unmarshal([]byte(jsonContractCall), &contractCall)
require.NoError(t, err)

res, err := EncodeContractCall(contractCall)
Expand Down
4 changes: 1 addition & 3 deletions ethcoder/ethcoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ func BytesToBytes32(slice []byte) [32]byte {
}

func PaddedAddress(address string) string {
if strings.HasPrefix(address, "0x") {
address = address[2:]
}
address = strings.TrimPrefix(address, "0x")
if len(address) < 64 {
address = strings.Repeat("0", 64-len(address)) + address
}
Expand Down
2 changes: 1 addition & 1 deletion ethcoder/solidity_pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func solidityArgumentPack(typ string, val interface{}, isArray bool) ([]byte, er
return nil, fmt.Errorf("not a [%d]byte", size)
}

v := make([]byte, size, size)
v := make([]byte, size)
var ok bool
for i := 0; i < int(size); i++ {
v[i], ok = rv.Index(i).Interface().(byte)
Expand Down
7 changes: 4 additions & 3 deletions ethcoder/typed_data_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (
"strings"

"github.com/0xsequence/ethkit/go-ethereum/common"
"github.com/bytedance/sonic"
)

func TypedDataFromJSON(typedDataJSON string) (*TypedData, error) {
var typedData TypedData
err := json.Unmarshal([]byte(typedDataJSON), &typedData)
err := sonic.ConfigFastest.Unmarshal([]byte(typedDataJSON), &typedData)
if err != nil {
return nil, err
}
Expand All @@ -34,7 +35,7 @@ func (t *TypedData) MarshalJSON() ([]byte, error) {
return nil, err
}

return json.Marshal(TypedDataJSON{
return sonic.ConfigFastest.Marshal(TypedDataJSON{
Types: t.Types,
PrimaryType: t.PrimaryType,
Domain: t.Domain,
Expand Down Expand Up @@ -130,7 +131,7 @@ func (t *TypedData) UnmarshalJSON(data []byte) error {
}

// Json decoder with json.Number support, so that we can decode big.Int values
dec := json.NewDecoder(bytes.NewReader(data))
dec := sonic.ConfigFastest.NewDecoder(bytes.NewReader(data))
dec.UseNumber()

var raw TypedDataRaw
Expand Down
Loading
Loading