Skip to content

Commit

Permalink
all: consider decimals on wrapped assets, fix VAA posting, fix solana…
Browse files Browse the repository at this point in the history
… account parsing
  • Loading branch information
hendrikhofstadt committed Aug 28, 2020
1 parent 8e6dc49 commit f6750a3
Show file tree
Hide file tree
Showing 34 changed files with 6,104 additions and 5,661 deletions.
88 changes: 44 additions & 44 deletions bridge/cmd/guardiand/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,53 +48,53 @@ func p2p(obsvC chan *gossipv1.LockupObservation, sendC chan []byte) func(ctx con

var idht *dht.IpfsDHT

h, err := libp2p.New(ctx,
// Use the keypair we generated
libp2p.Identity(priv),

// Multiple listen addresses
libp2p.ListenAddrStrings(
// Listen on QUIC only.
// TODO(leo): is this more or less stable than using both TCP and QUIC transports?
// https://github.com/libp2p/go-libp2p/issues/688
fmt.Sprintf("/ip4/0.0.0.0/udp/%d/quic", *p2pPort),
fmt.Sprintf("/ip6/::/udp/%d/quic", *p2pPort),
),

// Enable TLS security as the only security protocol.
libp2p.Security(libp2ptls.ID, libp2ptls.New),

// Enable QUIC transport as the only transport.
libp2p.Transport(libp2pquic.NewTransport),

// Let's prevent our peer from having too many
// connections by attaching a connection manager.
libp2p.ConnectionManager(connmgr.NewConnManager(
100, // Lowwater
400, // HighWater,
time.Minute, // GracePeriod
)),

// Let this host use the DHT to find other hosts
libp2p.Routing(func(h host.Host) (routing.PeerRouting, error) {
// TODO(leo): Persistent data store (i.e. address book)
idht, err = dht.New(ctx, h, dht.Mode(dht.ModeServer),
// TODO(leo): This intentionally makes us incompatible with the global IPFS DHT
dht.ProtocolPrefix(protocol.ID("/"+*p2pNetworkID)),
h, err := libp2p.New(ctx,
// Use the keypair we generated
libp2p.Identity(priv),

// Multiple listen addresses
libp2p.ListenAddrStrings(
// Listen on QUIC only.
// TODO(leo): is this more or less stable than using both TCP and QUIC transports?
// https://github.com/libp2p/go-libp2p/issues/688
fmt.Sprintf("/ip4/0.0.0.0/udp/%d/quic", *p2pPort),
fmt.Sprintf("/ip6/::/udp/%d/quic", *p2pPort),
),

// Enable TLS security as the only security protocol.
libp2p.Security(libp2ptls.ID, libp2ptls.New),

// Enable QUIC transport as the only transport.
libp2p.Transport(libp2pquic.NewTransport),

// Let's prevent our peer from having too many
// connections by attaching a connection manager.
libp2p.ConnectionManager(connmgr.NewConnManager(
100, // Lowwater
400, // HighWater,
time.Minute, // GracePeriod
)),

// Let this host use the DHT to find other hosts
libp2p.Routing(func(h host.Host) (routing.PeerRouting, error) {
// TODO(leo): Persistent data store (i.e. address book)
idht, err = dht.New(ctx, h, dht.Mode(dht.ModeServer),
// TODO(leo): This intentionally makes us incompatible with the global IPFS DHT
dht.ProtocolPrefix(protocol.ID("/"+*p2pNetworkID)),
)
return idht, err
}),
)
return idht, err
}),
)

if err != nil {
panic(err)
}
if err != nil {
panic(err)
}

defer func() {
// TODO: libp2p cannot be cleanly restarted (https://github.com/libp2p/go-libp2p/issues/992)
logger.Error("p2p routine has exited, cancelling root context...", zap.Error(re))
rootCtxCancel()
}()
defer func() {
// TODO: libp2p cannot be cleanly restarted (https://github.com/libp2p/go-libp2p/issues/992)
logger.Error("p2p routine has exited, cancelling root context...", zap.Error(re))
rootCtxCancel()
}()

logger.Info("Connecting to bootstrap peers", zap.String("bootstrap_peers", *p2pBootstrap))

Expand Down
16 changes: 9 additions & 7 deletions bridge/cmd/guardiand/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ func vaaConsensusProcessor(lockC chan *common.ChainLock, setC chan *common.Guard
SourceAddress: k.SourceAddress,
TargetAddress: k.TargetAddress,
Asset: &vaa.AssetMeta{
Chain: k.TokenChain,
Address: k.TokenAddress,
Chain: k.TokenChain,
Address: k.TokenAddress,
Decimals: k.TokenDecimals,
},
Amount: k.Amount,
},
Expand Down Expand Up @@ -281,10 +282,15 @@ func vaaConsensusProcessor(lockC chan *common.ChainLock, setC chan *common.Guard
zap.Any("vaa", signed),
zap.String("bytes", hex.EncodeToString(vaaBytes)))

if idx == 0 {
if idx == 1 {
vaaC <- signed
}
case t.TargetChain == vaa.ChainIDEthereum:
// cross-submit to Solana for data availability
if idx == 1 {
vaaC <- signed
}

timeout, cancel := context.WithTimeout(ctx, 15*time.Second)
tx, err := devnet.SubmitVAA(timeout, *ethRPC, signed)
cancel()
Expand All @@ -294,10 +300,6 @@ func vaaConsensusProcessor(lockC chan *common.ChainLock, setC chan *common.Guard
}
logger.Info("lockup submitted to Ethereum", zap.Any("tx", tx))

// cross-submit to Solana for data availability
if idx == 0 {
vaaC <- signed
}
default:
logger.Error("we don't know how to submit this VAA",
zap.String("digest", hash),
Expand Down
59 changes: 44 additions & 15 deletions bridge/cmd/vaa-test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type signerInfo struct {
index int
}

var i = 0

func main() {

keys := generateKeys(6)
Expand All @@ -38,8 +40,9 @@ func main() {
SourceAddress: vaa.Address{2, 1, 4},
TargetAddress: padAddress(devnet.GanacheClientDefaultAccountAddress),
Asset: &vaa.AssetMeta{
Chain: vaa.ChainIDSolana,
Address: hexToAddress("0x347ef34687bdc9f189e87a9200658d9c40e9988"),
Chain: vaa.ChainIDSolana,
Address: hexToAddress("0x347ef34687bdc9f189e87a9200658d9c40e9988"),
Decimals: 8,
},
Amount: big.NewInt(1000000000000000000),
},
Expand All @@ -56,8 +59,9 @@ func main() {
SourceAddress: vaa.Address{2, 1, 4},
TargetAddress: padAddress(devnet.GanacheClientDefaultAccountAddress),
Asset: &vaa.AssetMeta{
Chain: vaa.ChainIDEthereum,
Address: hexToAddress("0xd833215cbcc3f914bd1c9ece3ee7bf8b14f841bb"),
Chain: vaa.ChainIDEthereum,
Address: hexToAddress("0xd833215cbcc3f914bd1c9ece3ee7bf8b14f841bb"),
Decimals: 8,
},
Amount: big.NewInt(1000000000000000000),
},
Expand Down Expand Up @@ -98,8 +102,9 @@ func main() {
SourceAddress: vaa.Address{2, 1, 4},
TargetAddress: padAddress(devnet.GanacheClientDefaultAccountAddress),
Asset: &vaa.AssetMeta{
Chain: vaa.ChainIDSolana,
Address: hexToAddress("0x347ef34687bdc9f189e87a9200658d9c40e9988"),
Chain: vaa.ChainIDSolana,
Address: hexToAddress("0x347ef34687bdc9f189e87a9200658d9c40e9988"),
Decimals: 8,
},
Amount: big.NewInt(1000000000000000000),
},
Expand All @@ -116,8 +121,9 @@ func main() {
SourceAddress: vaa.Address{2, 1, 5},
TargetAddress: padAddress(devnet.GanacheClientDefaultAccountAddress),
Asset: &vaa.AssetMeta{
Chain: vaa.ChainIDSolana,
Address: hexToAddress("0x347ef34687bdc9f189e87a9200658d9c40e9988"),
Chain: vaa.ChainIDSolana,
Address: hexToAddress("0x347ef34687bdc9f189e87a9200658d9c40e9988"),
Decimals: 8,
},
Amount: big.NewInt(1000000000000000000),
},
Expand All @@ -134,13 +140,33 @@ func main() {
SourceAddress: vaa.Address{2, 1, 5},
TargetAddress: padAddress(devnet.GanacheClientDefaultAccountAddress),
Asset: &vaa.AssetMeta{
Chain: vaa.ChainIDSolana,
Address: hexToAddress("0x347ef34687bdc9f189e87a9200658d9c40e9988"),
Chain: vaa.ChainIDSolana,
Address: hexToAddress("0x347ef34687bdc9f189e87a9200658d9c40e9988"),
Decimals: 8,
},
Amount: big.NewInt(1000000000000000000),
},
}, []*signerInfo{{keys[1], 0}})

signAndPrintVAA(&vaa.VAA{
Version: 1,
GuardianSetIndex: 1,
Timestamp: time.Unix(2000, 0),
Payload: &vaa.BodyTransfer{
Nonce: 57,
SourceChain: 1,
TargetChain: 2,
SourceAddress: vaa.Address{2, 1, 5},
TargetAddress: padAddress(devnet.GanacheClientDefaultAccountAddress),
Asset: &vaa.AssetMeta{
Chain: vaa.ChainIDSolana,
Address: hexToAddress("0x347ef34687bdc9f189e87a9200658d9c40e9988"),
Decimals: 8,
},
Amount: big.NewInt(1000000000000000000),
},
}, []*signerInfo{{keys[0], 0}})

signAndPrintVAA(&vaa.VAA{
Version: 1,
GuardianSetIndex: 1,
Expand Down Expand Up @@ -169,8 +195,9 @@ func main() {
SourceAddress: vaa.Address{2, 1, 5},
TargetAddress: padAddress(devnet.GanacheClientDefaultAccountAddress),
Asset: &vaa.AssetMeta{
Chain: vaa.ChainIDSolana,
Address: hexToAddress("0x347ef34687bdc9f189e87a9200658d9c40e9988"),
Chain: vaa.ChainIDSolana,
Address: hexToAddress("0x347ef34687bdc9f189e87a9200658d9c40e9988"),
Decimals: 8,
},
Amount: big.NewInt(1000000000000000000),
},
Expand All @@ -187,8 +214,9 @@ func main() {
SourceAddress: vaa.Address{2, 1, 5},
TargetAddress: padAddress(devnet.GanacheClientDefaultAccountAddress),
Asset: &vaa.AssetMeta{
Chain: vaa.ChainIDSolana,
Address: hexToAddress("0x347ef34687bdc9f189e87a9200658d9c40e9988"),
Chain: vaa.ChainIDSolana,
Address: hexToAddress("0x347ef34687bdc9f189e87a9200658d9c40e9988"),
Decimals: 8,
},
Amount: big.NewInt(1000000000000000000),
},
Expand All @@ -203,7 +231,8 @@ func signAndPrintVAA(vaa *vaa.VAA, signers []*signerInfo) {
if err != nil {
panic(err)
}
println(hex.EncodeToString(vData))
println(i, hex.EncodeToString(vData))
i++
}

func generateKeys(n int) (keys []*ecdsa.PrivateKey) {
Expand Down
5 changes: 3 additions & 2 deletions bridge/pkg/common/chainlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ type ChainLock struct {
SourceChain vaa.ChainID
TargetChain vaa.ChainID

TokenChain vaa.ChainID
TokenAddress vaa.Address
TokenChain vaa.ChainID
TokenAddress vaa.Address
TokenDecimals uint8

Amount *big.Int
}
Loading

0 comments on commit f6750a3

Please sign in to comment.