Skip to content

Commit 5b74a9a

Browse files
committed
block hash generation by trie root of transactions, state root, results
1 parent 28f7221 commit 5b74a9a

File tree

4 files changed

+52
-42
lines changed

4 files changed

+52
-42
lines changed

chain/block.go

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
package chain
55

66
import (
7+
"bytes"
78
"context"
9+
"crypto/sha256"
810
"encoding/binary"
11+
"errors"
912
"fmt"
1013
"time"
1114

@@ -17,6 +20,7 @@ import (
1720
"github.com/ava-labs/avalanchego/utils/set"
1821
"github.com/ava-labs/avalanchego/vms/platformvm/warp"
1922
"github.com/ava-labs/avalanchego/x/merkledb"
23+
"github.com/cbergoon/merkletree"
2024
"go.opentelemetry.io/otel/attribute"
2125
oteltrace "go.opentelemetry.io/otel/trace"
2226
"go.uber.org/zap"
@@ -35,13 +39,36 @@ var (
3539
_ block.StateSummary = &SyncableBlock{}
3640
)
3741

42+
type TrieItem struct {
43+
x []byte
44+
}
45+
46+
// CalculateHash hashes the values of a TestContent
47+
func (t TrieItem) CalculateHash() ([]byte, error) {
48+
h := sha256.New()
49+
if _, err := h.Write([]byte(t.x)); err != nil {
50+
return nil, err
51+
}
52+
53+
return h.Sum(nil), nil
54+
}
55+
56+
// Equals tests for equality of two Contents
57+
func (t TrieItem) Equals(other merkletree.Content) (bool, error) {
58+
otherTI, ok := other.(TrieItem)
59+
if !ok {
60+
return false, errors.New("value is not of type TestContent")
61+
}
62+
return bytes.Equal(t.x, otherTI.x), nil
63+
}
64+
3865
type StatefulBlock struct {
3966
Prnt ids.ID `json:"parent"`
4067
Tmstmp int64 `json:"timestamp"`
4168
Hght uint64 `json:"height"`
4269

43-
Txs []*Transaction `json:"txs"`
44-
TxsRoot []byte `json:"txsRoot"`
70+
Txs []*Transaction `json:"txs"`
71+
BlockHash []byte `json:"txsRoot"`
4572

4673
// StateRoot is the root of the post-execution state
4774
// of [Prnt].
@@ -285,12 +312,32 @@ func (b *StatelessBlock) initializeBuilt(
285312
b.results = results
286313
b.feeManager = feeManager
287314
b.txsSet = set.NewSet[ids.ID](len(b.Txs))
315+
316+
// block hash generation
317+
var items2prove []merkletree.Content
318+
// state root
319+
items2prove = append(items2prove, TrieItem{x: b.StateRoot[:]})
320+
// transactions
288321
for _, tx := range b.Txs {
322+
items2prove = append(items2prove, TrieItem{x: tx.Bytes()})
289323
b.txsSet.Add(tx.ID())
290324
if tx.WarpMessage != nil {
291325
b.containsWarp = true
292326
}
293327
}
328+
// transaction results
329+
for _, result := range b.results {
330+
items2prove = append(items2prove, TrieItem{x: result.Output})
331+
}
332+
333+
trie, err := merkletree.NewTree(items2prove)
334+
// will only happen when no item is provided
335+
// never happen since at least one item(state root) is put in
336+
if err != nil {
337+
return err
338+
}
339+
b.BlockHash = trie.MerkleRoot()
340+
294341
return nil
295342
}
296343

chain/builder.go

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

66
import (
7-
"bytes"
87
"context"
98
"encoding/binary"
109
"errors"
@@ -18,12 +17,9 @@ import (
1817
"github.com/ava-labs/avalanchego/utils/logging"
1918
"github.com/ava-labs/avalanchego/utils/math"
2019
"github.com/ava-labs/avalanchego/utils/set"
21-
"github.com/cbergoon/merkletree"
2220
"go.opentelemetry.io/otel/attribute"
2321
"go.uber.org/zap"
2422

25-
"crypto/sha256"
26-
2723
"github.com/ava-labs/hypersdk/executor"
2824
"github.com/ava-labs/hypersdk/keys"
2925
"github.com/ava-labs/hypersdk/tstate"
@@ -38,29 +34,6 @@ const (
3834
stopBuildingThreshold = 2_048 // units
3935
)
4036

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-
6437
var errBlockFull = errors.New("block full")
6538

6639
func HandlePreExecute(log logging.Logger, err error) bool {
@@ -155,8 +128,6 @@ func BuildBlock(
155128
txsAttempted = 0
156129
results = []*Result{}
157130

158-
txSuccessList []merkletree.Content
159-
160131
vdrState = vm.ValidatorState()
161132
sm = vm.StateManager()
162133

@@ -165,9 +136,6 @@ func BuildBlock(
165136
prepareStreamLock sync.Mutex
166137
)
167138

168-
// prevent empty list
169-
txSuccessList = append(txSuccessList, TestContent{x: make([]byte, 32)})
170-
171139
// Batch fetch items from mempool to unblock incoming RPC/Gossip traffic
172140
mempool.StartStreaming(ctx)
173141
b.Txs = []*Transaction{}
@@ -413,7 +381,6 @@ func BuildBlock(
413381
tsv.Commit()
414382
b.Txs = append(b.Txs, tx)
415383
results = append(results, result)
416-
txSuccessList = append(txSuccessList, TestContent{x: tx.Bytes()})
417384
if tx.WarpMessage != nil {
418385
if warpErr == nil {
419386
// Add a bit if the warp message was verified
@@ -448,13 +415,6 @@ func BuildBlock(
448415
}
449416
}
450417

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-
458418
// Wait for stream preparation to finish to make
459419
// sure all transactions are returned to the mempool.
460420
go func() {

examples/tokenvm/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ require (
2828
github.com/bep/debounce v1.2.1 // indirect
2929
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
3030
github.com/btcsuite/btcd/btcutil v1.1.3 // indirect
31+
github.com/cbergoon/merkletree v0.2.0 // indirect
3132
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
3233
github.com/cespare/xxhash/v2 v2.2.0 // indirect
3334
github.com/chzyer/readline v1.5.1 // indirect

examples/tokenvm/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ github.com/btcsuite/snappy-go v0.0.0-20151229074030-0bdef8d06723/go.mod h1:8woku
9797
github.com/btcsuite/snappy-go v1.0.0/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg3lh6TiUghc=
9898
github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtEyQwv5/p4Mg4C0fgbePVuGr935/5ddU9Z3TmDRY=
9999
github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs=
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)