Skip to content

Commit

Permalink
fix genesis custodian pledge amount
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricfung committed Oct 12, 2023
1 parent f54d24a commit 20c3535
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 45 deletions.
3 changes: 2 additions & 1 deletion common/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"github.com/klauspost/compress/zstd"
)

// FIXME update this to the latest data format
// FIXME have a cmd to make some random transactions, snapshots and
// or other data to compress, then make different dicts respectively
//
//go:embed data/zstd.dic
var ZstdEmbed []byte
Expand Down
2 changes: 1 addition & 1 deletion common/custodian_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ type testCustodianStore struct {
custodianUpdateNodesTimestamp uint64
}

func (s *testCustodianStore) ReadCustodian(ts uint64) (*CustodianUpdateRequest, error) {
func (s *testCustodianStore) ReadCustodian(_ uint64) (*CustodianUpdateRequest, error) {
if s.custodianUpdateNodesExtra == nil {
if s.domain == nil {
return nil, nil
Expand Down
4 changes: 2 additions & 2 deletions common/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,8 @@ func (tx *Transaction) AddScriptOutput(accounts []*Address, s Script, amount Int

func (tx *Transaction) AddRandomScriptOutput(accounts []*Address, s Script, amount Integer) {
seed := make([]byte, 64)
_, err := rand.Read(seed)
if err != nil {
n, err := rand.Read(seed)
if err != nil || n != 64 {
panic(err)
}
tx.AddScriptOutput(accounts, s, amount, seed)
Expand Down
16 changes: 8 additions & 8 deletions common/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,39 +291,39 @@ func (store storeImpl) ReadUTXOLock(hash crypto.Hash, index int) (*UTXOWithLock,
return utxo, nil
}

func (store storeImpl) LockGhostKeys(keys []*crypto.Key, tx crypto.Hash, fork bool) error {
func (store storeImpl) LockGhostKeys(_ []*crypto.Key, _ crypto.Hash, _ bool) error {
return nil
}

func (store storeImpl) LockUTXOs(inputs []*Input, tx crypto.Hash, fork bool) error {
func (store storeImpl) LockUTXOs(_ []*Input, _ crypto.Hash, _ bool) error {
return nil
}

func (store storeImpl) ReadAllNodes(_ uint64, _ bool) []*Node {
return nil
}

func (store storeImpl) ReadTransaction(hash crypto.Hash) (*VersionedTransaction, string, error) {
func (store storeImpl) ReadTransaction(_ crypto.Hash) (*VersionedTransaction, string, error) {
return nil, "", nil
}

func (store storeImpl) CheckDepositInput(deposit *DepositData, tx crypto.Hash) error {
func (store storeImpl) CheckDepositInput(_ *DepositData, _ crypto.Hash) error {
return nil
}

func (store storeImpl) LockDepositInput(deposit *DepositData, tx crypto.Hash, fork bool) error {
func (store storeImpl) LockDepositInput(_ *DepositData, _ crypto.Hash, _ bool) error {
return nil
}

func (store storeImpl) ReadLastMintDistribution(batch uint64) (*MintDistribution, error) {
func (store storeImpl) ReadLastMintDistribution(_ uint64) (*MintDistribution, error) {
return nil, nil
}

func (store storeImpl) LockMintInput(mint *MintData, tx crypto.Hash, fork bool) error {
func (store storeImpl) LockMintInput(_ *MintData, _ crypto.Hash, _ bool) error {
return nil
}

func (store storeImpl) ReadCustodian(ts uint64) (*CustodianUpdateRequest, error) {
func (store storeImpl) ReadCustodian(_ uint64) (*CustodianUpdateRequest, error) {
return &CustodianUpdateRequest{Custodian: store.custodian}, nil
}

Expand Down
7 changes: 5 additions & 2 deletions crypto/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,11 @@ func (v *BatchVerifier) Verify() bool {
}

buf := make([]byte, 32)
rand.Read(buf[:16])
_, err := Rcoeffs[i].SetCanonicalBytes(buf)
n, err := rand.Read(buf[:16])
if err != nil || n != 16 {
panic(err)
}
_, err = Rcoeffs[i].SetCanonicalBytes(buf)
if err != nil {
return false
}
Expand Down
3 changes: 0 additions & 3 deletions kernel/custodian.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ func (node *Node) validateCustodianUpdateNodes(s *common.Snapshot, tx *common.Ve
if err != nil {
return err
}
if prev == nil {
panic("FIXME genesis check")
}
eh := crypto.Blake3Hash(tx.Extra[:len(tx.Extra)-64])
if !prev.Custodian.PublicSpendKey.Verify(eh, *curs.Signature) {
return fmt.Errorf("invalid custodian update approval signature %x", tx.Extra)
Expand Down
3 changes: 2 additions & 1 deletion kernel/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ func buildCustodianSnapshot(networkId crypto.Hash, epoch uint64, gns *Genesis) (
addr := common.NewAddressFromSeed(make([]byte, 64))
script := common.NewThresholdScript(64)
accounts := []*common.Address{&addr}
amount := common.NewInteger(100).Mul(len(gns.Nodes))
tx.Inputs = []*common.Input{{Genesis: networkId[:]}}
tx.AddOutputWithType(common.OutputTypeCustodianUpdateNodes, accounts, script, common.NewInteger(50000), seed)
tx.AddOutputWithType(common.OutputTypeCustodianUpdateNodes, accounts, script, amount, seed)

tx.Extra = append(tx.Extra, gns.Custodian.PublicSpendKey[:]...)
tx.Extra = append(tx.Extra, gns.Custodian.PublicViewKey[:]...)
Expand Down
2 changes: 1 addition & 1 deletion kernel/genesis_test.go

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions kernel/round.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,8 @@ func ComputeRoundHash(nodeId crypto.Hash, number uint64, snapshots []*common.Sna
}
}

var hash crypto.Hash
buf := binary.BigEndian.AppendUint64(nodeId[:], number)
hash = crypto.Blake3Hash(buf)
hash := crypto.Blake3Hash(buf)
for _, s := range snapshots {
if s.Version > version {
panic(nodeId)
Expand Down
48 changes: 24 additions & 24 deletions rpc/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ var (
)

func TestConsensus(t *testing.T) {
testConsensus(t, 0)
testConsensus(t)
}

func testConsensus(t *testing.T, snapVersionMint int) {
func testConsensus(t *testing.T) {
require := require.New(t)
kernel.TestMockReset()

Expand Down Expand Up @@ -109,13 +109,13 @@ func testConsensus(t *testing.T, snapVersionMint int) {
gt := testVerifyInfo(require, nodes)
require.Truef(gt.Timestamp.Before(epoch.Add(1*time.Second)), "%s should before %s", gt.Timestamp, epoch.Add(1*time.Second))

genesisAmount := 13442.5 / float64(INPUTS)
genesisAmount := (13439 + 3.5) / float64(INPUTS)
domainAddress := accounts[0].String()
deposits := make([]*common.VersionedTransaction, 0)
for i := 0; i < INPUTS; i++ {
raw := fmt.Sprintf(`{"version":5,"asset":"a99c2e0e2b1da4d648755ef19bd95139acbbe6564cfb06dec7cd34931ca72cdc","inputs":[{"deposit":{"chain":"8dd50817c082cdcdd6f167514928767a4b52426997bd6d4930eca101c5ff8a27","asset":"0xa974c709cfb4566686553a20790685a47aceaa33","transaction":"0xc7c1132b58e1f64c263957d7857fe5ec5294fce95d30dcd64efef71da1%06d","index":0,"amount":"%f"}}],"outputs":[{"type":0,"amount":"%f","script":"fffe01","accounts":["%s"]}]}`, i, genesisAmount, genesisAmount, domainAddress)
rand.Seed(time.Now().UnixNano())
tx, err := testSignTransaction(nodes[rand.Intn(len(nodes))].Host, accounts[0], raw, snapVersionMint)
tx, err := testSignTransaction(nodes[rand.Intn(len(nodes))].Host, accounts[0], raw)
require.Nil(err)
require.NotNil(tx)
deposits = append(deposits, &common.VersionedTransaction{SignedTransaction: *tx})
Expand All @@ -136,9 +136,9 @@ func testConsensus(t *testing.T, snapVersionMint int) {

utxos := make([]*common.VersionedTransaction, 0)
for _, d := range deposits {
raw := fmt.Sprintf(`{"version":2,"asset":"a99c2e0e2b1da4d648755ef19bd95139acbbe6564cfb06dec7cd34931ca72cdc","inputs":[{"hash":"%s","index":0}],"outputs":[{"type":0,"amount":"%f","script":"fffe01","accounts":["%s"]}]}`, d.PayloadHash().String(), genesisAmount, domainAddress)
raw := fmt.Sprintf(`{"version":5,"asset":"a99c2e0e2b1da4d648755ef19bd95139acbbe6564cfb06dec7cd34931ca72cdc","inputs":[{"hash":"%s","index":0}],"outputs":[{"type":0,"amount":"%f","script":"fffe01","accounts":["%s"]}]}`, d.PayloadHash().String(), genesisAmount, domainAddress)
rand.Seed(time.Now().UnixNano())
tx, err := testSignTransaction(nodes[rand.Intn(len(nodes))].Host, accounts[0], raw, snapVersionMint)
tx, err := testSignTransaction(nodes[rand.Intn(len(nodes))].Host, accounts[0], raw)
require.Nil(err)
require.NotNil(tx)
if tx != nil {
Expand Down Expand Up @@ -172,7 +172,7 @@ func testConsensus(t *testing.T, snapVersionMint int) {
require.Len(all, NODES)
require.Equal("ACCEPTED", all[NODES-1].State)

input, _ := testBuildPledgeInput(t, nodes, accounts[0], utxos, snapVersionMint)
input, _ := testBuildPledgeInput(t, nodes, accounts[0], utxos)
time.Sleep(3 * time.Second)
transactionsCount = transactionsCount + 1
tl, _ = testVerifySnapshots(require, nodes)
Expand All @@ -191,7 +191,7 @@ func testConsensus(t *testing.T, snapVersionMint int) {
legacy := time.Date(2023, time.Month(10), 31, 0, 0, 0, 0, time.UTC).Sub(epoch)
kernel.TestMockDiff(legacy)
for i := 0; i < 3; i++ {
dummyInputs = testSendDummyTransactionsWithRetry(t, nodes, accounts[0], dummyInputs, dummyAmount, snapVersionMint)
dummyInputs = testSendDummyTransactionsWithRetry(t, nodes, accounts[0], dummyInputs, dummyAmount)
transactionsCount = transactionsCount + len(dummyInputs)
}

Expand All @@ -207,14 +207,14 @@ func testConsensus(t *testing.T, snapVersionMint int) {
gt = testVerifyInfo(require, nodes)
require.Less(gt.Timestamp, epoch.Add(legacy).Add(61*time.Second))

pn, pi, sv := testPledgeNewNode(t, nodes, accounts[0], gdata, plist, input, root, snapVersionMint)
pn, pi, sv := testPledgeNewNode(t, nodes, accounts[0], gdata, plist, input, root)
t.Logf("PLEDGE %s\n", pn.Signer)
transactionsCount = transactionsCount + 1
defer pi.Teardown()
defer sv.Close()

for i := 0; i < 5; i++ {
dummyInputs = testSendDummyTransactionsWithRetry(t, nodes, accounts[0], dummyInputs, dummyAmount, snapVersionMint)
dummyInputs = testSendDummyTransactionsWithRetry(t, nodes, accounts[0], dummyInputs, dummyAmount)
transactionsCount = transactionsCount + len(dummyInputs)
}

Expand Down Expand Up @@ -300,7 +300,7 @@ func testConsensus(t *testing.T, snapVersionMint int) {
require.Equal("XINCtoRSJYrNNQUv3xTsptxDKRqwHMwtNkvsQwFS58oFXYvgu9QhoetNwbmxUQ4JJGcjR1gnttMau1nCmGpkSimHR1dxrP8u", payee.String())
nodes = testRemoveNode(nodes, signer)
for i := 0; i < 3; i++ {
dummyInputs = testSendDummyTransactionsWithRetry(t, nodes, accounts[0], dummyInputs, dummyAmount, snapVersionMint)
dummyInputs = testSendDummyTransactionsWithRetry(t, nodes, accounts[0], dummyInputs, dummyAmount)
transactionsCount = transactionsCount + len(dummyInputs)
}
transactionsCount = transactionsCount + 1
Expand Down Expand Up @@ -329,7 +329,7 @@ func testConsensus(t *testing.T, snapVersionMint int) {
require.Greater(hr.Round, uint64(1))

removalInputs := []*common.Input{{Hash: all[NODES].Transaction, Index: 0}}
removalInputs = testSendDummyTransactionsWithRetry(t, nodes[:1], payee, removalInputs, "13439", snapVersionMint)
removalInputs = testSendDummyTransactionsWithRetry(t, nodes[:1], payee, removalInputs, "13439")
transactionsCount = transactionsCount + 1
tl, _ = testVerifySnapshots(require, nodes)
require.Equal(transactionsCount, len(tl))
Expand Down Expand Up @@ -388,7 +388,7 @@ func testCustodianUpdateNodes(t *testing.T, nodes []*Node, signers, payees []com

raw := fmt.Sprintf(`{"version":5,"asset":"a99c2e0e2b1da4d648755ef19bd95139acbbe6564cfb06dec7cd34931ca72cdc","inputs":[{"deposit":{"chain":"8dd50817c082cdcdd6f167514928767a4b52426997bd6d4930eca101c5ff8a27","asset":"0xa974c709cfb4566686553a20790685a47aceaa33","transaction":"0xc7c1132b58e1f64c263957d7857fe5ec5294fce95d30dcd64efef71da1%06d","index":0,"amount":"%s"}}],"outputs":[{"type":0,"amount":"%s","script":"fffe01","accounts":["%s"]}]}`, 13439, amount.String(), amount.String(), domain.String())
rand.Seed(time.Now().UnixNano())
deposit, err := testSignTransaction(nodes[0].Host, domain, raw, 0)
deposit, err := testSignTransaction(nodes[0].Host, domain, raw)
require.Nil(err)
require.NotNil(deposit)
deposits := []*common.VersionedTransaction{{SignedTransaction: *deposit}}
Expand All @@ -412,7 +412,7 @@ func testCustodianUpdateNodes(t *testing.T, nodes []*Node, signers, payees []com
"outputs": outputs,
"extra": hex.EncodeToString(tx.Extra),
})
signed, err := testSignTransaction(nodes[0].Host, domain, string(rb), 0)
signed, err := testSignTransaction(nodes[0].Host, domain, string(rb))
require.Nil(err)
require.NotNil(signed)

Expand Down Expand Up @@ -492,8 +492,8 @@ func testRemoveNode(nodes []*Node, r common.Address) []*Node {
return tmp
}

func testSendDummyTransactionsWithRetry(t *testing.T, nodes []*Node, domain common.Address, inputs []*common.Input, amount string, snapVersionMint int) []*common.Input {
outputs := testSendDummyTransactions(nodes, domain, inputs, amount, snapVersionMint)
func testSendDummyTransactionsWithRetry(t *testing.T, nodes []*Node, domain common.Address, inputs []*common.Input, amount string) []*common.Input {
outputs := testSendDummyTransactions(nodes, domain, inputs, amount)
time.Sleep(3 * time.Second)

var missingInputs []*common.Input
Expand All @@ -515,12 +515,12 @@ func testSendDummyTransactionsWithRetry(t *testing.T, nodes []*Node, domain comm
missingNodes = append(missingNodes, nodes[i])
}
if len(missingInputs) > 0 {
testSendDummyTransactionsWithRetry(t, missingNodes, domain, missingInputs, amount, snapVersionMint)
testSendDummyTransactionsWithRetry(t, missingNodes, domain, missingInputs, amount)
}
return outputs
}

func testSendDummyTransactions(nodes []*Node, domain common.Address, inputs []*common.Input, amount string, snapVersionMint int) []*common.Input {
func testSendDummyTransactions(nodes []*Node, domain common.Address, inputs []*common.Input, amount string) []*common.Input {
outputs := make([]*common.Input, len(inputs))

var wg sync.WaitGroup
Expand All @@ -541,7 +541,7 @@ func testSendDummyTransactions(nodes []*Node, domain common.Address, inputs []*c
"accounts": []string{domain.String()},
}},
})
tx, _ := testSignTransaction(node.Host, domain, string(raw), snapVersionMint)
tx, _ := testSignTransaction(node.Host, domain, string(raw))
ver := common.VersionedTransaction{SignedTransaction: *tx}
id, _ := testSendTransaction(node.Host, hex.EncodeToString(ver.Marshal()))
var res map[string]string
Expand All @@ -568,7 +568,7 @@ metric = true
peers = [%s]
`

func testPledgeNewNode(t *testing.T, nodes []*Node, domain common.Address, genesisData []byte, plist, input, root string, snapVersionMint int) (Node, *kernel.Node, *http.Server) {
func testPledgeNewNode(t *testing.T, nodes []*Node, domain common.Address, genesisData []byte, plist, input, root string) (Node, *kernel.Node, *http.Server) {
require := require.New(t)
var signer, payee common.Address

Expand Down Expand Up @@ -604,7 +604,7 @@ func testPledgeNewNode(t *testing.T, nodes []*Node, domain common.Address, genes
}},
"extra": signer.PublicSpendKey.String() + payee.PublicSpendKey.String(),
})
tx, err := testSignTransaction(nodes[0].Host, domain, string(raw), snapVersionMint)
tx, err := testSignTransaction(nodes[0].Host, domain, string(raw))
require.Nil(err)
ver := common.VersionedTransaction{SignedTransaction: *tx}
testSendTransactionsToNodesWithRetry(t, nodes, []*common.VersionedTransaction{&ver})
Expand All @@ -628,7 +628,7 @@ func testPledgeNewNode(t *testing.T, nodes []*Node, domain common.Address, genes
return Node{Signer: signer, Payee: payee, Host: "127.0.0.1:18099"}, pnode, server
}

func testBuildPledgeInput(t *testing.T, nodes []*Node, domain common.Address, utxos []*common.VersionedTransaction, snapVersionMint int) (string, error) {
func testBuildPledgeInput(t *testing.T, nodes []*Node, domain common.Address, utxos []*common.VersionedTransaction) (string, error) {
require := require.New(t)
inputs := []map[string]any{}
for _, tx := range utxos {
Expand Down Expand Up @@ -658,7 +658,7 @@ func testBuildPledgeInput(t *testing.T, nodes []*Node, domain common.Address, ut
"inputs": inputs,
"outputs": outputs,
})
tx, err := testSignTransaction(nodes[0].Host, domain, string(raw), snapVersionMint)
tx, err := testSignTransaction(nodes[0].Host, domain, string(raw))
require.Nil(err)
ver := common.VersionedTransaction{SignedTransaction: *tx}
testSendTransactionsToNodesWithRetry(t, nodes, []*common.VersionedTransaction{&ver})
Expand Down Expand Up @@ -787,7 +787,7 @@ func setupTestNet(root string) ([]common.Address, []common.Address, []byte, stri
return signers, payees, genesisData, peersList
}

func testSignTransaction(node string, account common.Address, rawStr string, snapVersionMint int) (*common.SignedTransaction, error) {
func testSignTransaction(node string, account common.Address, rawStr string) (*common.SignedTransaction, error) {
var raw signerInput
err := json.Unmarshal([]byte(rawStr), &raw)
if err != nil {
Expand Down

0 comments on commit 20c3535

Please sign in to comment.