Skip to content
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

WIP: Fix/message parsing #81

Merged
merged 9 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
30 changes: 15 additions & 15 deletions core/contracts/replicamanager/replicamanager.abigen.go

Large diffs are not rendered by default.

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions core/contracts/test/headerharness/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package headerharness

//go:generate go run github.com/synapsecns/sanguine/tools/abigen generate --sol ../../../../packages/contracts/flattened/HeaderHarness.sol --pkg headerharness --sol-version 0.8.13 --filename headerharness
// line after go:generate cannot be left blank
1,418 changes: 1,418 additions & 0 deletions core/contracts/test/headerharness/headerharness.abigen.go

Large diffs are not rendered by default.

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions core/contracts/test/headerharness/headerharness.metadata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions core/contracts/test/headerharness/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package headerharness

import (
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
)

// HeaderHarnessRef is a header harness reference
//nolint: golint
type HeaderHarnessRef struct {
*HeaderHarness
address common.Address
}

// Address gets the address of the contract.
func (h HeaderHarnessRef) Address() common.Address {
return h.address
}

// NewHeaderHarnessRef creates a new header harness.
func NewHeaderHarnessRef(address common.Address, backend bind.ContractBackend) (*HeaderHarnessRef, error) {
contract, err := NewHeaderHarness(address, backend)
if err != nil {
return nil, fmt.Errorf("could not create header harness: %w", err)
}

return &HeaderHarnessRef{
HeaderHarness: contract,
address: address,
}, nil
}

var _ vm.ContractRef = &HeaderHarnessRef{}
510 changes: 476 additions & 34 deletions core/contracts/test/messageharness/messageharness.abigen.go

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/contracts/updatermanager/generate.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package updatermanager

//go:generate go run github.com/synapsecns/synapse-node/testutils/codegen/abigen generate --sol ../../../packages/contracts/build/UpdaterManager.sol --pkg updatermanager --sol-version 0.8.13 --filename updatemanager
//go:generate go run github.com/synapsecns/synapse-node/testutils/codegen/abigen generate --sol ../../../packages/contracts/flattened/UpdaterManager.sol --pkg updatermanager --sol-version 0.8.13 --filename updatemanager
5,737 changes: 3,141 additions & 2,596 deletions core/contracts/updatermanager/updatemanager.abigen.go

Large diffs are not rendered by default.

Large diffs are not rendered by default.

6,864 changes: 3,746 additions & 3,118 deletions core/contracts/xappconfig/xappconfig.abigen.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/contracts/xappconfig/xappconfig.contractinfo.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions core/db/datastore/sql/base/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ func (s Store) StoreCommittedMessage(ctx context.Context, domainID uint32, messa
CMLeafIndex: message.LeafIndex(),
CMMessage: message.Message(),
CMLeaf: hashToSlice(message.Leaf()),
CMOrigin: decodedMessage.Origin(),
CMOrigin: decodedMessage.OriginDomain(),
CMSender: hashToSlice(decodedMessage.Sender()),
CMNonce: decodedMessage.Nonce(),
CMDestination: decodedMessage.Destination(),
CMDestination: decodedMessage.DestinationDomain(),
CMRecipient: hashToSlice(decodedMessage.Recipient()),
CMBody: decodedMessage.Body(),
CMOptimisticSeconds: decodedMessage.OptimisticSeconds(),
Expand Down
7 changes: 4 additions & 3 deletions core/db/datastore/sql/base/model.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package base

import (
"math/big"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/synapsecns/sanguine/core/types"
"gorm.io/gorm"
"math/big"
"time"
)

// define common field names. See package docs for an explanation of why we have to do this.
Expand Down Expand Up @@ -175,7 +176,7 @@ func (c CommittedMessage) Encode() ([]byte, error) {

var _ types.CommittedMessage = CommittedMessage{}

var _ types.Message = CommittedMessage{}
// var _ types.Message = CommittedMessage{}
Copy link
Contributor

Choose a reason for hiding this comment

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

what is the purpose of this? is it okay to comment it out


// SignedAttestation stores attestations.
type SignedAttestation struct {
Expand Down
7 changes: 5 additions & 2 deletions core/db/db_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package db_test

import (
"math/big"

"github.com/brianvoe/gofakeit/v6"
"github.com/ethereum/go-ethereum/common"
. "github.com/stretchr/testify/assert"
"github.com/synapsecns/sanguine/core/db"
"github.com/synapsecns/sanguine/core/types"
"math/big"
)

func (t *DBSuite) TestRetrieveLatestNonce() {
Expand All @@ -20,7 +21,9 @@ func (t *DBSuite) TestRetrieveLatestNonce() {
leafIndex := uint32(1)

for i := 0; i < 10; i++ {
realMessage := types.NewMessage(10, common.BigToHash(big.NewInt(gofakeit.Int64())), uint32(nonce), gofakeit.Uint32(), []byte(gofakeit.Sentence(10)), common.BigToHash(big.NewInt(gofakeit.Int64())))
realTips := types.NewTips(big.NewInt(gofakeit.Int64()), big.NewInt(gofakeit.Int64()), big.NewInt(gofakeit.Int64()), big.NewInt(gofakeit.Int64()))
realHeader := types.NewHeader(gofakeit.Uint32(), common.BigToHash(big.NewInt(gofakeit.Int64())), uint32(nonce), gofakeit.Uint32(), common.BigToHash(big.NewInt(gofakeit.Int64())), gofakeit.Uint32())
realMessage := types.NewMessage(realHeader, realTips, []byte(gofakeit.Sentence(10)))

encoded, err := types.EncodeMessage(realMessage)
Nil(t.T(), err)
Expand Down
5 changes: 5 additions & 0 deletions core/testutil/contracttype.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/synapsecns/sanguine/core/contracts/home"
"github.com/synapsecns/sanguine/core/contracts/replicamanager"
"github.com/synapsecns/sanguine/core/contracts/test/attestationharness"
"github.com/synapsecns/sanguine/core/contracts/test/headerharness"
"github.com/synapsecns/sanguine/core/contracts/test/homeharness"
"github.com/synapsecns/sanguine/core/contracts/test/messageharness"
"github.com/synapsecns/sanguine/core/contracts/test/replicamanagerharness"
Expand Down Expand Up @@ -55,6 +56,8 @@ const (
AttestationHarnessType contractTypeImpl = iota
// TipsHarnessType is the type of the tips harness.
TipsHarnessType contractTypeImpl = iota
// HeaderHarnessType is the tyoe of the header harness.
HeaderHarnessType contractTypeImpl = iota
// ReplicaManagerHarnessType is the replica manager harness type.
ReplicaManagerHarnessType contractTypeImpl = iota
// UpdaterManagerType is the type of the update manager.
Expand Down Expand Up @@ -104,6 +107,8 @@ func (c contractTypeImpl) ContractInfo() *compiler.Contract {
return attestationcollector.Contracts["solidity/AttestationCollector.sol:AttestationCollector"]
case ReplicaManagerType:
return replicamanager.Contracts["solidity/ReplicaManager.sol:ReplicaManager"]
case HeaderHarnessType:
return headerharness.Contracts["solidity/HeaderHarness.sol:HeaderHarness"]
default:
panic("not yet implemented")
}
Expand Down
13 changes: 7 additions & 6 deletions core/testutil/contracttypeimpl_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions core/testutil/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/synapsecns/sanguine/core/contracts/test/attestationharness"
"github.com/synapsecns/sanguine/core/contracts/test/headerharness"
"github.com/synapsecns/sanguine/core/contracts/test/homeharness"
"github.com/synapsecns/sanguine/core/contracts/test/messageharness"
"github.com/synapsecns/sanguine/core/contracts/test/replicamanagerharness"
Expand Down Expand Up @@ -111,3 +112,22 @@ func (r ReplicaManagerHarnessDeployer) Deploy(ctx context.Context) (backends.Dep
return replicamanagerharness.NewReplicaManagerHarnessRef(address, backend)
})
}

// HeaderHarnessDeployer deploys the header harness.
type HeaderHarnessDeployer struct {
*deployer.BaseDeployer
}

// NewHeaderHarnessDeployer gets the header harness.
func NewHeaderHarnessDeployer(registry deployer.GetOnlyContractRegistry, backend backends.SimulatedTestBackend) deployer.ContractDeployer {
return HeaderHarnessDeployer{deployer.NewSimpleDeployer(registry, backend, HeaderHarnessType)}
}

// Deploy deploys the header harness.
func (h HeaderHarnessDeployer) Deploy(ctx context.Context) (backends.DeployedContract, error) {
return h.DeploySimpleContract(ctx, func(transactOps *bind.TransactOpts, backend bind.ContractBackend) (common.Address, *types.Transaction, interface{}, error) {
return headerharness.DeployHeaderHarness(transactOps, backend)
}, func(address common.Address, backend bind.ContractBackend) (interface{}, error) {
return headerharness.NewHeaderHarnessRef(address, backend)
})
}
2 changes: 1 addition & 1 deletion core/testutil/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func NewDeployManager(t *testing.T) *DeployManager {
t.Helper()

parentManager := manager.NewDeployerManager(t, NewHomeDeployer, NewXAppConfigDeployer, NewMessageHarnessDeployer, NewHomeHarnessDeployer,
NewUpdateManagerDeployer, NewAttestationCollectorDeployer, NewAttestationHarnessDeployer, NewTipsHarnessDeployer, NewReplicaManagerDeployer, NewReplicaManagerHarnessDeployer)
NewUpdateManagerDeployer, NewAttestationCollectorDeployer, NewAttestationHarnessDeployer, NewTipsHarnessDeployer, NewReplicaManagerDeployer, NewReplicaManagerHarnessDeployer, NewHeaderHarnessDeployer)
return &DeployManager{parentManager}
}

Expand Down
12 changes: 12 additions & 0 deletions core/testutil/typecast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testutil

import (
"context"
"github.com/synapsecns/sanguine/core/contracts/test/headerharness"

"github.com/stretchr/testify/assert"
"github.com/synapsecns/sanguine/core/contracts/attestationcollector"
Expand Down Expand Up @@ -131,3 +132,14 @@ func (d *DeployManager) GetTipsHarness(ctx context.Context, backend backends.Sim

return tipsContract, tipsHarness
}

// GetHeaderHarness gets the header harness.
func (d *DeployManager) GetHeaderHarness(ctx context.Context, backend backends.SimulatedTestBackend) (contract backends.DeployedContract, handle *headerharness.HeaderHarnessRef) {
d.T().Helper()

headerHarnessContract := d.GetContractRegistry(backend).Get(ctx, HeaderHarnessType)
headerHarness, ok := headerHarnessContract.ContractHandle().(*headerharness.HeaderHarnessRef)
assert.True(d.T(), ok)

return headerHarnessContract, headerHarness
}
7 changes: 7 additions & 0 deletions core/testutil/typecast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,10 @@ func (s SimulatedSuite) TestTypecastReplicaManagerHarness() {
NotNil(s.T(), replicaManagerHarness)
})
}

func (s SimulatedSuite) TestTypecastHeaderHarness() {
NotPanics(s.T(), func() {
_, headerHarness := s.deployManager.GetHeaderHarness(s.GetTestContext(), s.testBackend)
NotNil(s.T(), headerHarness)
})
}
72 changes: 60 additions & 12 deletions core/types/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"bytes"
"encoding/binary"
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
"github.com/libs4go/crypto/ecdsa"
"math/big"
)

// EncodeSignedAttestation encodes a signed attestation.
Expand Down Expand Up @@ -110,7 +111,8 @@ func DecodeAttestation(toDecode []byte) (Attestation, error) {
}

const (
tipsVersion = uint16(1)
tipsVersion uint16 = 1
//nolint: staticcheck
offsetUpdater = 2
offsetRelayer = 14
offsetProver = 26
Expand Down Expand Up @@ -142,33 +144,79 @@ func DecodeTips(toDecode []byte) (Tips, error) {
return NewTips(updaterTip, relayerTip, proverTip, processorTip), nil
}

// messageEncoder contains the binary structore of the message.
type messageEncoder struct {
Origin uint32
type headerEncoder struct {
Version uint16
OriginDomain uint32
Sender [32]byte
Nonce uint32
Destination uint32
DestinationDomain uint32
Recipient [32]byte
OptimisticSeconds uint32
}

// EncodeHeader encodes a message header.
func EncodeHeader(header Header) ([]byte, error) {
newHeader := headerEncoder{
Version: header.Version(),
OriginDomain: header.OriginDomain(),
Sender: header.Sender(),
Nonce: header.Nonce(),
DestinationDomain: header.DestinationDomain(),
Recipient: header.Recipient(),
OptimisticSeconds: header.OptimisticSeconds(),
}

buf := new(bytes.Buffer)

err := binary.Write(buf, binary.BigEndian, newHeader)
if err != nil {
return nil, fmt.Errorf("could not write binary: %w", err)
}

return buf.Bytes(), nil
}

// messageEncoder contains the binary structore of the message.
type messageEncoder struct {
Version uint16
HeaderOffset uint16
TipsOffset uint16
BodyOffset uint16
}

// EncodeMessage encodes a message.
func EncodeMessage(m Message) ([]byte, error) {
encodedHeader, err := EncodeHeader(m.Header())
if err != nil {
return []byte{}, fmt.Errorf("could not encode header: %w", err)
}

encodedTips, err := EncodeTips(m.Tips())
if err != nil {
return []byte{}, fmt.Errorf("could not encode tips: %w", err)
}

tipsOffset := headerOffset + uint16(len(encodedHeader))
bodyOffset := tipsOffset + uint16(len(encodedTips))

// payload := append(append(encodedHeader, encodedTips...), m.Body()...)

newMessage := messageEncoder{
Origin: m.Origin(),
Sender: m.Sender(),
Nonce: m.Nonce(),
Destination: m.Destination(),
OptimisticSeconds: m.OptimisticSeconds(),
Version: m.Version(),
HeaderOffset: headerOffset,
TipsOffset: tipsOffset,
BodyOffset: bodyOffset,
}

buf := new(bytes.Buffer)

err := binary.Write(buf, binary.BigEndian, newMessage)
err = binary.Write(buf, binary.BigEndian, newMessage)
if err != nil {
return nil, fmt.Errorf("could not write binary: %w", err)
}

buf.Write(encodedHeader)
buf.Write(encodedTips)
buf.Write(m.Body())

return buf.Bytes(), nil
Expand Down
Loading