Skip to content

Commit

Permalink
fix : lint
Browse files Browse the repository at this point in the history
  • Loading branch information
0xsharma committed Dec 16, 2023
1 parent d1bbfdd commit c2c859d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 25 deletions.
56 changes: 38 additions & 18 deletions avail/avail.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,25 @@ import (
"time"

availConfig "github.com/0xPolygonHermez/zkevm-node/avail/internal/config"
availTypes "github.com/0xPolygonHermez/zkevm-node/avail/types"
"github.com/0xPolygonHermez/zkevm-node/log"
"golang.org/x/crypto/sha3"

gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4"
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"

availTypes "github.com/0xPolygonHermez/zkevm-node/avail/types"
"golang.org/x/crypto/sha3"
)

// AccountNextIndexRPCResponse represents the next index of account rpc response
type AccountNextIndexRPCResponse struct {
Result uint `json:"result"`
}

// DataProofRPCResponse represents the data proof rpc response
type DataProofRPCResponse struct {
Result DataProof `json:"result"`
}

// DataProof represents the data proof structure
type DataProof struct {
Root string `json:"root"`
Proof []string `json:"proof"`
Expand All @@ -36,15 +37,23 @@ type DataProof struct {
Leaf string `json:"leaf"`
}

var Config availConfig.Config
var Api *gsrpc.SubstrateAPI
var Meta *types.Metadata
var AppId int
var GenesisHash types.Hash
var Rv *types.RuntimeVersion
var KeyringPair signature.KeyringPair
var DestinationAddress types.Hash
var DestinationDomain types.UCompact
// nolint : revive
var (
Config availConfig.Config
Api *gsrpc.SubstrateAPI
Meta *types.Metadata
AppId int
GenesisHash types.Hash
Rv *types.RuntimeVersion
KeyringPair signature.KeyringPair
DestinationAddress types.Hash
DestinationDomain types.UCompact
)

const (
networkID = 42
defaultTip = 1000
)

func init() {
err := Config.GetConfig("/app/avail-config.json")
Expand Down Expand Up @@ -79,7 +88,7 @@ func init() {
log.Fatalf("cannot get runtime version:%w", err)
}

KeyringPair, err = signature.KeyringPairFromSecret(Config.Seed, 42)
KeyringPair, err = signature.KeyringPairFromSecret(Config.Seed, networkID)
if err != nil {
log.Fatalf("cannot create keypair:%w", err)
}
Expand All @@ -92,6 +101,7 @@ func init() {
DestinationDomain = types.NewUCompactFromUInt(uint64(Config.DestinationDomain))
}

// PostData posts data to the avail DA
func PostData(txData []byte) (*availTypes.BatchDAData, error) {
log.Infof("⚡️ Prepared data for Avail:%d bytes", len(txData))

Expand All @@ -114,7 +124,7 @@ func PostData(txData []byte) (*availTypes.BatchDAData, error) {
GenesisHash: GenesisHash,
Nonce: nonce,
SpecVersion: Rv.SpecVersion,
Tip: types.NewUCompactFromUInt(1000),
Tip: types.NewUCompactFromUInt(defaultTip),
AppID: types.NewUCompactFromUInt(uint64(AppId)),
TransactionVersion: Rv.TransactionVersion,
}
Expand Down Expand Up @@ -185,7 +195,9 @@ out:
}

var dataProofResp DataProofRPCResponse
json.Unmarshal(data, &dataProofResp)
if err := json.Unmarshal(data, &dataProofResp); err != nil {
return nil, fmt.Errorf("cannot unmarshal data proof:%v", err)
}

if dataProofResp.Result.Leaf == fmt.Sprintf("%#x", batchHash) {
dataProof = dataProofResp.Result
Expand All @@ -205,11 +217,14 @@ out:
}

batchDAData.BlockNumber = uint(header.Number)

//nolint:errcheck
GetData(uint64(header.Number), dataProof.LeafIndex)
log.Infof("🟢 prepared DA data:%+v", batchDAData)
return &batchDAData, nil
}

// DispatchDataRoot dispatches the data root to the avail DA
func DispatchDataRoot(blockNumber uint64) error {
blockHash, err := Api.RPC.Chain.GetBlockHash(blockNumber)
if err != nil {
Expand Down Expand Up @@ -240,7 +255,7 @@ func DispatchDataRoot(blockNumber uint64) error {
GenesisHash: GenesisHash,
Nonce: nonce,
SpecVersion: Rv.SpecVersion,
Tip: types.NewUCompactFromUInt(1000),
Tip: types.NewUCompactFromUInt(defaultTip),
AppID: types.NewUCompactFromUInt(0),
TransactionVersion: Rv.TransactionVersion,
}
Expand Down Expand Up @@ -282,6 +297,7 @@ out:
return nil
}

// GetData gets the data from the avail DA block
func GetData(blockNumber uint64, index uint) ([]byte, error) {
blockHash, err := Api.RPC.Chain.GetBlockHash(uint64(blockNumber))
if err != nil {
Expand All @@ -303,6 +319,7 @@ func GetData(blockNumber uint64, index uint) ([]byte, error) {
return data[index], nil
}

// GetAccountNextIndex gets the next index of the account from avail DA
func GetAccountNextIndex() (types.UCompact, error) {
resp, err := http.Post("https://goldberg.avail.tools/api", "application/json", strings.NewReader(fmt.Sprintf("{\"id\":1,\"jsonrpc\":\"2.0\",\"method\":\"system_accountNextIndex\",\"params\":[\"%v\"]}", KeyringPair.Address)))
if err != nil {
Expand All @@ -317,7 +334,10 @@ func GetAccountNextIndex() (types.UCompact, error) {
}

var accountNextIndex AccountNextIndexRPCResponse
json.Unmarshal(data, &accountNextIndex)

if err := json.Unmarshal(data, &accountNextIndex); err != nil {
return types.NewUCompactFromUInt(0), fmt.Errorf("cannot unmarshal account next index:%v", err)
}

return types.NewUCompactFromUInt(uint64(accountNextIndex.Result)), nil
}
3 changes: 2 additions & 1 deletion avail/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
)

// Config represents the configuration for avail DA integration
type Config struct {
Seed string `json:"seed"`
ApiURL string `json:"api_url"`
Expand All @@ -16,8 +17,8 @@ type Config struct {
Timeout int `json:"timeout"`
}

// GetConfig reads the configuration from a file
func (c *Config) GetConfig(configFileName string) error {

jsonFile, err := os.Open(configFileName)
if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions avail/types/BatchDAData.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"reflect"
)

// BatchDAData represents the data of a batch
type BatchDAData struct {
BlockNumber uint
Proof []string `json:"proof"`
Width uint `json:"number_of_leaves"`
LeafIndex uint `json:"leaf_index"`
}

// IsEmpty returns true if the BatchDAData is empty
func (b BatchDAData) IsEmpty() bool {
return reflect.DeepEqual(b, BatchDAData{})
}
1 change: 1 addition & 0 deletions ethtxmanager/ethtxmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func (c *Client) Add(ctx context.Context, owner, id string, from common.Address,
return err
}

// nolint : gomnd
gasPrice = gasPrice.Add(gasPrice, gasPrice.Div(gasPrice, big.NewInt(10)))

// create monitored tx
Expand Down
14 changes: 8 additions & 6 deletions sequencesender/sequencesender.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import (
)

const (
ethTxManagerOwner = "sequencer"
monitoredIDFormat = "sequence-from-%v-to-%v"
ethTxManagerOwner = "sequencer"
monitoredIDFormat = "sequence-from-%v-to-%v"
l1_rpcPollInterval = 10 * time.Second
hourSecondsCount = 3600
)

var (
Expand Down Expand Up @@ -195,18 +197,18 @@ func (s *SequenceSender) getSequencesToSend(ctx context.Context) ([]types.Sequen
} else if len(batch.BatchL2Data) != 0 && *dataRoot == [32]byte{} {
log.Infof("⏰ Data root is not available yet for batch %d, skipping", batch.BatchNumber)
// this is to prevent from calling the L1 RPC too often
time.Sleep(10 * time.Second)
time.Sleep(l1_rpcPollInterval)
// if a data root is not available after an hour, something is wrong, re-dispatch the data root
if time.Now().Unix()-batch.Timestamp.Unix() > 3600 {
if time.Now().Unix()-batch.Timestamp.Unix() > hourSecondsCount {
// check data redispatch map
redispatchTime := s.redispatchDataMap[batch.BatchNumber]
if redispatchTime == 0 || time.Now().Unix()-redispatchTime > 3600 {
if redispatchTime == 0 || time.Now().Unix()-redispatchTime > hourSecondsCount {
err := avail.DispatchDataRoot(uint64(batch.DABlockNumber))
if err == nil {
s.redispatchDataMap[batch.BatchNumber] = time.Now().Unix()
} else {
log.Warnf("error dispatching data root for batch %d: %v", batch.BatchNumber, err)
time.Sleep(10 * time.Second)
time.Sleep(l1_rpcPollInterval)
}
}
}
Expand Down

0 comments on commit c2c859d

Please sign in to comment.