Skip to content

Commit 28f7221

Browse files
committed
root generation
1 parent bfe1fcd commit 28f7221

File tree

6 files changed

+48
-1
lines changed

6 files changed

+48
-1
lines changed

chain/block.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ type StatefulBlock struct {
4040
Tmstmp int64 `json:"timestamp"`
4141
Hght uint64 `json:"height"`
4242

43-
Txs []*Transaction `json:"txs"`
43+
Txs []*Transaction `json:"txs"`
44+
TxsRoot []byte `json:"txsRoot"`
4445

4546
// StateRoot is the root of the post-execution state
4647
// of [Prnt].

chain/builder.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package chain
55

66
import (
7+
"bytes"
78
"context"
89
"encoding/binary"
910
"errors"
@@ -17,9 +18,12 @@ import (
1718
"github.com/ava-labs/avalanchego/utils/logging"
1819
"github.com/ava-labs/avalanchego/utils/math"
1920
"github.com/ava-labs/avalanchego/utils/set"
21+
"github.com/cbergoon/merkletree"
2022
"go.opentelemetry.io/otel/attribute"
2123
"go.uber.org/zap"
2224

25+
"crypto/sha256"
26+
2327
"github.com/ava-labs/hypersdk/executor"
2428
"github.com/ava-labs/hypersdk/keys"
2529
"github.com/ava-labs/hypersdk/tstate"
@@ -34,6 +38,29 @@ const (
3438
stopBuildingThreshold = 2_048 // units
3539
)
3640

41+
type TestContent struct {
42+
x []byte
43+
}
44+
45+
// CalculateHash hashes the values of a TestContent
46+
func (t TestContent) CalculateHash() ([]byte, error) {
47+
h := sha256.New()
48+
if _, err := h.Write([]byte(t.x)); err != nil {
49+
return nil, err
50+
}
51+
52+
return h.Sum(nil), nil
53+
}
54+
55+
// Equals tests for equality of two Contents
56+
func (t TestContent) Equals(other merkletree.Content) (bool, error) {
57+
otherTC, ok := other.(TestContent)
58+
if !ok {
59+
return false, errors.New("value is not of type TestContent")
60+
}
61+
return bytes.Equal(t.x, otherTC.x), nil
62+
}
63+
3764
var errBlockFull = errors.New("block full")
3865

3966
func HandlePreExecute(log logging.Logger, err error) bool {
@@ -128,6 +155,8 @@ func BuildBlock(
128155
txsAttempted = 0
129156
results = []*Result{}
130157

158+
txSuccessList []merkletree.Content
159+
131160
vdrState = vm.ValidatorState()
132161
sm = vm.StateManager()
133162

@@ -136,6 +165,9 @@ func BuildBlock(
136165
prepareStreamLock sync.Mutex
137166
)
138167

168+
// prevent empty list
169+
txSuccessList = append(txSuccessList, TestContent{x: make([]byte, 32)})
170+
139171
// Batch fetch items from mempool to unblock incoming RPC/Gossip traffic
140172
mempool.StartStreaming(ctx)
141173
b.Txs = []*Transaction{}
@@ -381,6 +413,7 @@ func BuildBlock(
381413
tsv.Commit()
382414
b.Txs = append(b.Txs, tx)
383415
results = append(results, result)
416+
txSuccessList = append(txSuccessList, TestContent{x: tx.Bytes()})
384417
if tx.WarpMessage != nil {
385418
if warpErr == nil {
386419
// Add a bit if the warp message was verified
@@ -415,6 +448,13 @@ func BuildBlock(
415448
}
416449
}
417450

451+
trie, err := merkletree.NewTree(txSuccessList)
452+
if err != nil {
453+
b.vm.Logger().Warn("unable to build trie", zap.Error(err))
454+
return nil, err
455+
}
456+
b.TxsRoot = trie.MerkleRoot()
457+
418458
// Wait for stream preparation to finish to make
419459
// sure all transactions are returned to the mempool.
420460
go func() {

examples/morpheusvm/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ require (
2424
github.com/beorn7/perks v1.0.1 // indirect
2525
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
2626
github.com/btcsuite/btcd/btcutil v1.1.3 // indirect
27+
github.com/cbergoon/merkletree v0.2.0 // indirect
2728
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
2829
github.com/cespare/xxhash/v2 v2.2.0 // indirect
2930
github.com/chzyer/readline v1.5.1 // indirect

examples/morpheusvm/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ github.com/btcsuite/snappy-go v0.0.0-20151229074030-0bdef8d06723/go.mod h1:8woku
9595
github.com/btcsuite/snappy-go v1.0.0/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg3lh6TiUghc=
9696
github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtEyQwv5/p4Mg4C0fgbePVuGr935/5ddU9Z3TmDRY=
9797
github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs=
98+
github.com/cbergoon/merkletree v0.2.0 h1:Bttqr3OuoiZEo4ed1L7fTasHka9II+BF9fhBfbNEEoQ=
99+
github.com/cbergoon/merkletree v0.2.0/go.mod h1:5c15eckUgiucMGDOCanvalj/yJnD+KAZj1qyJtRW5aM=
98100
github.com/cenkalti/backoff/v4 v4.2.0 h1:HN5dHm3WBOgndBH6E8V0q2jIYIR3s9yglV8k/+MN3u4=
99101
github.com/cenkalti/backoff/v4 v4.2.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
100102
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ require (
4343
github.com/beorn7/perks v1.0.1 // indirect
4444
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
4545
github.com/btcsuite/btcd/btcutil v1.1.3 // indirect
46+
github.com/cbergoon/merkletree v0.2.0 // indirect
4647
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
4748
github.com/cespare/xxhash/v2 v2.2.0 // indirect
4849
github.com/chzyer/readline v1.5.1 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtE
9797
github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs=
9898
github.com/bytecodealliance/wasmtime-go/v14 v14.0.0 h1:ur7S3P+PAeJmgllhSrKnGQOAmmtUbLQxb/nw2NZiaEM=
9999
github.com/bytecodealliance/wasmtime-go/v14 v14.0.0/go.mod h1:tqOVEUjnXY6aGpSfM9qdVRR6G//Yc513fFYUdzZb/DY=
100+
github.com/cbergoon/merkletree v0.2.0 h1:Bttqr3OuoiZEo4ed1L7fTasHka9II+BF9fhBfbNEEoQ=
101+
github.com/cbergoon/merkletree v0.2.0/go.mod h1:5c15eckUgiucMGDOCanvalj/yJnD+KAZj1qyJtRW5aM=
100102
github.com/cenkalti/backoff/v4 v4.2.0 h1:HN5dHm3WBOgndBH6E8V0q2jIYIR3s9yglV8k/+MN3u4=
101103
github.com/cenkalti/backoff/v4 v4.2.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
102104
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=

0 commit comments

Comments
 (0)