Skip to content
This repository has been archived by the owner on Jan 3, 2023. It is now read-only.

Bitcoin Checkpointing MVP #83

Draft
wants to merge 23 commits into
base: eudico
Choose a base branch
from
Draft
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
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,11 @@ bin/ipget
bin/tmp/*
.idea
scratchpad

gen.gen
*.key
data/*/*
!data/*/keystore/
!data/*/config.toml
!data/*/token
!data/*/share.toml
105 changes: 105 additions & 0 deletions chain/checkpointing/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package checkpointing

import (
"context"
"fmt"

"github.com/Zondax/multi-party-sig/pkg/protocol"
pubsub "github.com/libp2p/go-libp2p-pubsub"
)

type Network struct {
sub *pubsub.Subscription
topic *pubsub.Topic
}

func NewNetwork(sub *pubsub.Subscription, topic *pubsub.Topic) *Network {
c := &Network{
sub: sub,
topic: topic,
}
return c
}

func (n *Network) Next(ctx context.Context) *protocol.Message {
msg, err := n.sub.Next(ctx)
if err == context.Canceled {
// We are actually done and don't want to wait for messages anymore
return nil
}

if err != nil {
panic(err)
}

// Unwrapping protocol message from PubSub message
// see https://pkg.go.dev/github.com/libp2p/[email protected]/pb#Message
// https://pkg.go.dev/github.com/taurusgroup/[email protected]/pkg/protocol?utm_source=gopls#Message
var pmessage protocol.Message
err = pmessage.UnmarshalBinary(msg.Data)
if err != nil {
panic(err)
}

return &pmessage
}

func (n *Network) Send(ctx context.Context, msg *protocol.Message) {
data, err := msg.MarshalBinary()
if err != nil {
panic(err)
}
err = n.topic.Publish(ctx, data)
if err != nil {
panic(err)
}
}

/*
Handling incoming and outgoing messages
*/

func broadcastingMessage(ctx context.Context, h protocol.Handler, network *Network, over chan bool) {
for {
msg, ok := <-h.Listen()
fmt.Println("Outgoing message:", msg)
if !ok {
// the channel was closed, indicating that the protocol is done executing.
close(over)
return
}
network.Send(ctx, msg)
}
}

func waitingMessages(ctx context.Context, h protocol.Handler, network *Network, over chan bool) {
for {
select {
case <-over:
return
default:
msg := network.Next(ctx)
fmt.Println("Incoming message:", msg, h.CanAccept(msg))
/*if h.CanAccept(msg) {
// This message is ours
fmt.Println("Incoming message:", msg)
}*/
h.Accept(msg)
}

}
}

func LoopHandler(ctx context.Context, h protocol.Handler, network *Network) {
over := make(chan bool)

ctx, cancel := context.WithCancel(ctx)
defer cancel()

go broadcastingMessage(ctx, h, network, over)
go waitingMessages(ctx, h, network, over)

<-over

fmt.Println("We are done")
}
56 changes: 56 additions & 0 deletions chain/checkpointing/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package checkpointing

import (
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/minio/minio-go/v7"
)

func StoreMinersConfig(ctx context.Context, minioClient *minio.Client, bucketName, hash string) error {
filename := hash + ".txt"
filePath := "/tmp/" + filename
contentType := "text/plain"

_, err := minioClient.FPutObject(ctx, bucketName, filename, filePath, minio.PutObjectOptions{ContentType: contentType})
if err != nil {
return err
}

return nil
}

func CreateMinersConfig(data []byte) ([]byte, error) {
hash := sha256.Sum256(data)

err := os.WriteFile("/tmp/"+hex.EncodeToString(hash[:])+".txt", data, 0644)
if err != nil {
return nil, err
}

return hash[:], nil
}

func GetMinersConfig(ctx context.Context, minioClient *minio.Client, bucketName, hash string) (string, error) {
filename := hash + ".txt"
filePath := "/tmp/verification/" + filename

err := minioClient.FGetObject(context.Background(), bucketName, filename, filePath, minio.GetObjectOptions{})
if err != nil {
return "", err
}

content, err := ioutil.ReadFile(filePath) // the file is inside the local directory
if err != nil {
return "", err
}
cpCid := strings.Split(string(content), "\n")[0]
fmt.Println(cpCid)

return cpCid, nil
}
Loading